web/lib/Zend/Barcode/Renderer/RendererAbstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Barcode
       
    17  * @subpackage Renderer
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: RendererAbstract.php 22999 2010-09-23 19:43:14Z mikaelkael $
       
    21  */
       
    22 
       
    23 /**
       
    24  * Class for rendering the barcode
       
    25  *
       
    26  * @category   Zend
       
    27  * @package    Zend_Barcode
       
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    30  */
       
    31 abstract class Zend_Barcode_Renderer_RendererAbstract
       
    32 {
       
    33     /**
       
    34      * Namespace of the renderer for autoloading
       
    35      * @var string
       
    36      */
       
    37     protected $_rendererNamespace = 'Zend_Barcode_Renderer';
       
    38 
       
    39     /**
       
    40      * Renderer type
       
    41      * @var string
       
    42      */
       
    43     protected $_type = null;
       
    44 
       
    45     /**
       
    46      * Activate/Deactivate the automatic rendering of exception
       
    47      * @var boolean
       
    48      */
       
    49     protected $_automaticRenderError = false;
       
    50 
       
    51     /**
       
    52      * Offset of the barcode from the top of the rendering resource
       
    53      * @var integer
       
    54      */
       
    55     protected $_topOffset = 0;
       
    56 
       
    57     /**
       
    58      * Offset of the barcode from the left of the rendering resource
       
    59      * @var integer
       
    60      */
       
    61     protected $_leftOffset = 0;
       
    62 
       
    63     /**
       
    64      * Horizontal position of the barcode in the rendering resource
       
    65      * @var integer
       
    66      */
       
    67     protected $_horizontalPosition = 'left';
       
    68 
       
    69     /**
       
    70      * Vertical position of the barcode in the rendering resource
       
    71      * @var integer
       
    72      */
       
    73     protected $_verticalPosition = 'top';
       
    74 
       
    75     /**
       
    76      * Module size rendering
       
    77      * @var float
       
    78      */
       
    79     protected $_moduleSize = 1;
       
    80 
       
    81     /**
       
    82      * Barcode object
       
    83      * @var Zend_Barcode_Object_ObjectAbstract
       
    84      */
       
    85     protected $_barcode;
       
    86 
       
    87     /**
       
    88      * Drawing resource
       
    89      */
       
    90     protected $_resource;
       
    91 
       
    92     /**
       
    93      * Constructor
       
    94      * @param array|Zend_Config $options
       
    95      * @return void
       
    96      */
       
    97     public function __construct($options = null)
       
    98     {
       
    99         if ($options instanceof Zend_Config) {
       
   100             $options = $options->toArray();
       
   101         }
       
   102         if (is_array($options)) {
       
   103             $this->setOptions($options);
       
   104         }
       
   105         $this->_type = strtolower(substr(
       
   106             get_class($this),
       
   107             strlen($this->_rendererNamespace) + 1
       
   108         ));
       
   109     }
       
   110 
       
   111     /**
       
   112      * Set renderer state from options array
       
   113      * @param  array $options
       
   114      * @return Zend_Renderer_Object
       
   115      */
       
   116     public function setOptions($options)
       
   117     {
       
   118         foreach ($options as $key => $value) {
       
   119             $method = 'set' . $key;
       
   120             if (method_exists($this, $method)) {
       
   121                 $this->$method($value);
       
   122             }
       
   123         }
       
   124         return $this;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Set renderer state from config object
       
   129      * @param Zend_Config $config
       
   130      * @return Zend_Renderer_Object
       
   131      */
       
   132     public function setConfig(Zend_Config $config)
       
   133     {
       
   134         return $this->setOptions($config->toArray());
       
   135     }
       
   136 
       
   137     /**
       
   138      * Set renderer namespace for autoloading
       
   139      *
       
   140      * @param string $namespace
       
   141      * @return Zend_Renderer_Object
       
   142      */
       
   143     public function setRendererNamespace($namespace)
       
   144     {
       
   145         $this->_rendererNamespace = $namespace;
       
   146         return $this;
       
   147     }
       
   148 
       
   149     /**
       
   150      * Retrieve renderer namespace
       
   151      *
       
   152      * @return string
       
   153      */
       
   154     public function getRendererNamespace()
       
   155     {
       
   156         return $this->_rendererNamespace;
       
   157     }
       
   158 
       
   159     /**
       
   160      * Retrieve renderer type
       
   161      * @return string
       
   162      */
       
   163     public function getType()
       
   164     {
       
   165         return $this->_type;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Manually adjust top position
       
   170      * @param integer $value
       
   171      * @return Zend_Barcode_Renderer
       
   172      * @throw Zend_Barcode_Renderer_Exception
       
   173      */
       
   174     public function setTopOffset($value)
       
   175     {
       
   176         if (!is_numeric($value) || intval($value) < 0) {
       
   177             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   178             throw new Zend_Barcode_Renderer_Exception(
       
   179                 'Vertical position must be greater than or equals 0'
       
   180             );
       
   181         }
       
   182         $this->_topOffset = intval($value);
       
   183         return $this;
       
   184     }
       
   185 
       
   186     /**
       
   187      * Retrieve vertical adjustment
       
   188      * @return integer
       
   189      */
       
   190     public function getTopOffset()
       
   191     {
       
   192         return $this->_topOffset;
       
   193     }
       
   194 
       
   195     /**
       
   196      * Manually adjust left position
       
   197      * @param integer $value
       
   198      * @return Zend_Barcode_Renderer
       
   199      * @throw Zend_Barcode_Renderer_Exception
       
   200      */
       
   201     public function setLeftOffset($value)
       
   202     {
       
   203         if (!is_numeric($value) || intval($value) < 0) {
       
   204             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   205             throw new Zend_Barcode_Renderer_Exception(
       
   206                 'Horizontal position must be greater than or equals 0'
       
   207             );
       
   208         }
       
   209         $this->_leftOffset = intval($value);
       
   210         return $this;
       
   211     }
       
   212 
       
   213     /**
       
   214      * Retrieve vertical adjustment
       
   215      * @return integer
       
   216      */
       
   217     public function getLeftOffset()
       
   218     {
       
   219         return $this->_leftOffset;
       
   220     }
       
   221 
       
   222     /**
       
   223      * Activate/Deactivate the automatic rendering of exception
       
   224      * @param boolean $value
       
   225      */
       
   226     public function setAutomaticRenderError($value)
       
   227     {
       
   228         $this->_automaticRenderError = (bool) $value;
       
   229         return $this;
       
   230     }
       
   231 
       
   232     /**
       
   233      * Horizontal position of the barcode in the rendering resource
       
   234      * @param string $value
       
   235      * @return Zend_Barcode_Renderer
       
   236      * @throw Zend_Barcode_Renderer_Exception
       
   237      */
       
   238     public function setHorizontalPosition($value)
       
   239     {
       
   240         if (!in_array($value, array('left' , 'center' , 'right'))) {
       
   241             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   242             throw new Zend_Barcode_Renderer_Exception(
       
   243                 "Invalid barcode position provided must be 'left', 'center' or 'right'"
       
   244             );
       
   245         }
       
   246         $this->_horizontalPosition = $value;
       
   247         return $this;
       
   248     }
       
   249 
       
   250     /**
       
   251      * Horizontal position of the barcode in the rendering resource
       
   252      * @return string
       
   253      */
       
   254     public function getHorizontalPosition()
       
   255     {
       
   256         return $this->_horizontalPosition;
       
   257     }
       
   258 
       
   259     /**
       
   260      * Vertical position of the barcode in the rendering resource
       
   261      * @param string $value
       
   262      * @return Zend_Barcode_Renderer
       
   263      * @throw Zend_Barcode_Renderer_Exception
       
   264      */
       
   265     public function setVerticalPosition($value)
       
   266     {
       
   267         if (!in_array($value, array('top' , 'middle' , 'bottom'))) {
       
   268             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   269             throw new Zend_Barcode_Renderer_Exception(
       
   270                 "Invalid barcode position provided must be 'top', 'middle' or 'bottom'"
       
   271             );
       
   272         }
       
   273         $this->_verticalPosition = $value;
       
   274         return $this;
       
   275     }
       
   276 
       
   277     /**
       
   278      * Vertical position of the barcode in the rendering resource
       
   279      * @return string
       
   280      */
       
   281     public function getVerticalPosition()
       
   282     {
       
   283         return $this->_verticalPosition;
       
   284     }
       
   285 
       
   286     /**
       
   287      * Set the size of a module
       
   288      * @param float $value
       
   289      * @return Zend_Barcode_Renderer
       
   290      * @throw Zend_Barcode_Renderer_Exception
       
   291      */
       
   292     public function setModuleSize($value)
       
   293     {
       
   294         if (!is_numeric($value) || floatval($value) <= 0) {
       
   295             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   296             throw new Zend_Barcode_Renderer_Exception(
       
   297                 'Float size must be greater than 0'
       
   298             );
       
   299         }
       
   300         $this->_moduleSize = floatval($value);
       
   301         return $this;
       
   302     }
       
   303 
       
   304 
       
   305     /**
       
   306      * Set the size of a module
       
   307      * @return float
       
   308      */
       
   309     public function getModuleSize()
       
   310     {
       
   311         return $this->_moduleSize;
       
   312     }
       
   313 
       
   314     /**
       
   315      * Retrieve the automatic rendering of exception
       
   316      * @return boolean
       
   317      */
       
   318     public function getAutomaticRenderError()
       
   319     {
       
   320         return $this->_automaticRenderError;
       
   321     }
       
   322 
       
   323     /**
       
   324      * Set the barcode object
       
   325      * @param Zend_Barcode_Object $barcode
       
   326      * @return Zend_Barcode_Renderer
       
   327      */
       
   328     public function setBarcode($barcode)
       
   329     {
       
   330         if (!$barcode instanceof Zend_Barcode_Object_ObjectAbstract) {
       
   331             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   332             throw new Zend_Barcode_Renderer_Exception(
       
   333                 'Invalid barcode object provided to setBarcode()'
       
   334             );
       
   335         }
       
   336         $this->_barcode = $barcode;
       
   337         return $this;
       
   338     }
       
   339 
       
   340     /**
       
   341      * Retrieve the barcode object
       
   342      * @return Zend_Barcode_Object
       
   343      */
       
   344     public function getBarcode()
       
   345     {
       
   346         return $this->_barcode;
       
   347     }
       
   348 
       
   349     /**
       
   350      * Checking of parameters after all settings
       
   351      * @return boolean
       
   352      */
       
   353     public function checkParams()
       
   354     {
       
   355         $this->_checkBarcodeObject();
       
   356         $this->_checkParams();
       
   357         return true;
       
   358     }
       
   359 
       
   360     /**
       
   361      * Check if a barcode object is correctly provided
       
   362      * @return void
       
   363      * @throw Zend_Barcode_Renderer_Exception
       
   364      */
       
   365     protected function _checkBarcodeObject()
       
   366     {
       
   367         if ($this->_barcode === null) {
       
   368             /**
       
   369              * @see Zend_Barcode_Renderer_Exception
       
   370              */
       
   371             require_once 'Zend/Barcode/Renderer/Exception.php';
       
   372             throw new Zend_Barcode_Renderer_Exception(
       
   373                 'No barcode object provided'
       
   374             );
       
   375         }
       
   376     }
       
   377 
       
   378     /**
       
   379      * Calculate the left and top offset of the barcode in the
       
   380      * rendering support
       
   381      *
       
   382      * @param float $supportHeight
       
   383      * @param float $supportWidth
       
   384      * @return void
       
   385      */
       
   386     protected function _adjustPosition($supportHeight, $supportWidth)
       
   387     {
       
   388         $barcodeHeight = $this->_barcode->getHeight(true) * $this->_moduleSize;
       
   389         if ($barcodeHeight != $supportHeight && $this->_topOffset == 0) {
       
   390             switch ($this->_verticalPosition) {
       
   391                 case 'middle':
       
   392                     $this->_topOffset = floor(
       
   393                             ($supportHeight - $barcodeHeight) / 2);
       
   394                     break;
       
   395                 case 'bottom':
       
   396                     $this->_topOffset = $supportHeight - $barcodeHeight;
       
   397                     break;
       
   398                 case 'top':
       
   399                 default:
       
   400                     $this->_topOffset = 0;
       
   401                     break;
       
   402             }
       
   403         }
       
   404         $barcodeWidth = $this->_barcode->getWidth(true) * $this->_moduleSize;
       
   405         if ($barcodeWidth != $supportWidth && $this->_leftOffset == 0) {
       
   406             switch ($this->_horizontalPosition) {
       
   407                 case 'center':
       
   408                     $this->_leftOffset = floor(
       
   409                             ($supportWidth - $barcodeWidth) / 2);
       
   410                     break;
       
   411                 case 'right':
       
   412                     $this->_leftOffset = $supportWidth - $barcodeWidth;
       
   413                     break;
       
   414                 case 'left':
       
   415                 default:
       
   416                     $this->_leftOffset = 0;
       
   417                     break;
       
   418             }
       
   419         }
       
   420     }
       
   421 
       
   422     /**
       
   423      * Draw the barcode in the rendering resource
       
   424      * @return mixed
       
   425      */
       
   426     public function draw()
       
   427     {
       
   428         try {
       
   429             $this->checkParams();
       
   430             $this->_initRenderer();
       
   431             $this->_drawInstructionList();
       
   432         } catch (Zend_Exception $e) {
       
   433             $renderable = false;
       
   434             if ($e instanceof Zend_Barcode_Exception) {
       
   435                 $renderable = $e->isRenderable();
       
   436             }
       
   437             if ($this->_automaticRenderError && $renderable) {
       
   438                 $barcode = Zend_Barcode::makeBarcode(
       
   439                     'error',
       
   440                     array('text' => $e->getMessage())
       
   441                 );
       
   442                 $this->setBarcode($barcode);
       
   443                 $this->_resource = null;
       
   444                 $this->_initRenderer();
       
   445                 $this->_drawInstructionList();
       
   446             } else {
       
   447                 if ($e instanceof Zend_Barcode_Exception) {
       
   448                     $e->setIsRenderable(false);
       
   449                 }
       
   450                 throw $e;
       
   451             }
       
   452         }
       
   453         return $this->_resource;
       
   454     }
       
   455 
       
   456     /**
       
   457      * Sub process to draw the barcode instructions
       
   458      * Needed by the automatic error rendering
       
   459      */
       
   460     private function _drawInstructionList()
       
   461     {
       
   462         $instructionList = $this->_barcode->draw();
       
   463         foreach ($instructionList as $instruction) {
       
   464             switch ($instruction['type']) {
       
   465                 case 'polygon':
       
   466                     $this->_drawPolygon(
       
   467                         $instruction['points'],
       
   468                         $instruction['color'],
       
   469                         $instruction['filled']
       
   470                     );
       
   471                     break;
       
   472                 case 'text': //$text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
       
   473                     $this->_drawText(
       
   474                         $instruction['text'],
       
   475                         $instruction['size'],
       
   476                         $instruction['position'],
       
   477                         $instruction['font'],
       
   478                         $instruction['color'],
       
   479                         $instruction['alignment'],
       
   480                         $instruction['orientation']
       
   481                     );
       
   482                     break;
       
   483                 default:
       
   484                     /**
       
   485                      * @see Zend_Barcode_Renderer_Exception
       
   486                      */
       
   487                     require_once 'Zend/Barcode/Renderer/Exception.php';
       
   488                     throw new Zend_Barcode_Renderer_Exception(
       
   489                         'Unkown drawing command'
       
   490                     );
       
   491             }
       
   492         }
       
   493     }
       
   494 
       
   495     /**
       
   496      * Checking of parameters after all settings
       
   497      * @return void
       
   498      */
       
   499     abstract protected function _checkParams();
       
   500 
       
   501     /**
       
   502      * Render the resource by sending headers and drawed resource
       
   503      * @return mixed
       
   504      */
       
   505     abstract public function render();
       
   506 
       
   507     /**
       
   508      * Initialize the rendering resource
       
   509      * @return void
       
   510      */
       
   511     abstract protected function _initRenderer();
       
   512 
       
   513     /**
       
   514      * Draw a polygon in the rendering resource
       
   515      * @param array $points
       
   516      * @param integer $color
       
   517      * @param boolean $filled
       
   518      */
       
   519     abstract protected function _drawPolygon($points, $color, $filled = true);
       
   520 
       
   521     /**
       
   522      * Draw a polygon in the rendering resource
       
   523      * @param string $text
       
   524      * @param float $size
       
   525      * @param array $position
       
   526      * @param string $font
       
   527      * @param integer $color
       
   528      * @param string $alignment
       
   529      * @param float $orientation
       
   530      */
       
   531     abstract protected function _drawText(
       
   532         $text,
       
   533         $size,
       
   534         $position,
       
   535         $font,
       
   536         $color,
       
   537         $alignment = 'center',
       
   538         $orientation = 0
       
   539     );
       
   540 }