File indexing completed on 2025-01-26 05:29:17
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 /** 0024 * Class for rendering the barcode 0025 * 0026 * @category Zend 0027 * @package Zend_Barcode 0028 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0029 * @license http://framework.zend.com/license/new-bsd New BSD License 0030 */ 0031 abstract class Zend_Barcode_Renderer_RendererAbstract 0032 { 0033 /** 0034 * Namespace of the renderer for autoloading 0035 * @var string 0036 */ 0037 protected $_rendererNamespace = 'Zend_Barcode_Renderer'; 0038 0039 /** 0040 * Renderer type 0041 * @var string 0042 */ 0043 protected $_type = null; 0044 0045 /** 0046 * Activate/Deactivate the automatic rendering of exception 0047 * @var boolean 0048 */ 0049 protected $_automaticRenderError = false; 0050 0051 /** 0052 * Offset of the barcode from the top of the rendering resource 0053 * @var integer 0054 */ 0055 protected $_topOffset = 0; 0056 0057 /** 0058 * Offset of the barcode from the left of the rendering resource 0059 * @var integer 0060 */ 0061 protected $_leftOffset = 0; 0062 0063 /** 0064 * Horizontal position of the barcode in the rendering resource 0065 * @var integer 0066 */ 0067 protected $_horizontalPosition = 'left'; 0068 0069 /** 0070 * Vertical position of the barcode in the rendering resource 0071 * @var integer 0072 */ 0073 protected $_verticalPosition = 'top'; 0074 0075 /** 0076 * Module size rendering 0077 * @var float 0078 */ 0079 protected $_moduleSize = 1; 0080 0081 /** 0082 * Barcode object 0083 * @var Zend_Barcode_Object_ObjectAbstract 0084 */ 0085 protected $_barcode; 0086 0087 /** 0088 * Drawing resource 0089 */ 0090 protected $_resource; 0091 0092 /** 0093 * Constructor 0094 * 0095 * @param array|Zend_Config $options 0096 * @return Zend_Barcode_Renderer_RendererAbstract 0097 */ 0098 public function __construct($options = null) 0099 { 0100 if ($options instanceof Zend_Config) { 0101 $options = $options->toArray(); 0102 } 0103 if (is_array($options)) { 0104 $this->setOptions($options); 0105 } 0106 $this->_type = strtolower(substr( 0107 get_class($this), 0108 strlen($this->_rendererNamespace) + 1 0109 )); 0110 } 0111 0112 /** 0113 * Set renderer state from options array 0114 * @param array $options 0115 * @return Zend_Renderer_Object 0116 */ 0117 public function setOptions($options) 0118 { 0119 foreach ($options as $key => $value) { 0120 $method = 'set' . $key; 0121 if (method_exists($this, $method)) { 0122 $this->$method($value); 0123 } 0124 } 0125 return $this; 0126 } 0127 0128 /** 0129 * Set renderer state from config object 0130 * @param Zend_Config $config 0131 * @return Zend_Renderer_Object 0132 */ 0133 public function setConfig(Zend_Config $config) 0134 { 0135 return $this->setOptions($config->toArray()); 0136 } 0137 0138 /** 0139 * Set renderer namespace for autoloading 0140 * 0141 * @param string $namespace 0142 * @return Zend_Renderer_Object 0143 */ 0144 public function setRendererNamespace($namespace) 0145 { 0146 $this->_rendererNamespace = $namespace; 0147 return $this; 0148 } 0149 0150 /** 0151 * Retrieve renderer namespace 0152 * 0153 * @return string 0154 */ 0155 public function getRendererNamespace() 0156 { 0157 return $this->_rendererNamespace; 0158 } 0159 0160 /** 0161 * Retrieve renderer type 0162 * @return string 0163 */ 0164 public function getType() 0165 { 0166 return $this->_type; 0167 } 0168 0169 /** 0170 * Manually adjust top position 0171 * @param integer $value 0172 * @return Zend_Barcode_Renderer 0173 * @throws Zend_Barcode_Renderer_Exception 0174 */ 0175 public function setTopOffset($value) 0176 { 0177 if (!is_numeric($value) || intval($value) < 0) { 0178 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0179 throw new Zend_Barcode_Renderer_Exception( 0180 'Vertical position must be greater than or equals 0' 0181 ); 0182 } 0183 $this->_topOffset = intval($value); 0184 return $this; 0185 } 0186 0187 /** 0188 * Retrieve vertical adjustment 0189 * @return integer 0190 */ 0191 public function getTopOffset() 0192 { 0193 return $this->_topOffset; 0194 } 0195 0196 /** 0197 * Manually adjust left position 0198 * @param integer $value 0199 * @return Zend_Barcode_Renderer 0200 * @throws Zend_Barcode_Renderer_Exception 0201 */ 0202 public function setLeftOffset($value) 0203 { 0204 if (!is_numeric($value) || intval($value) < 0) { 0205 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0206 throw new Zend_Barcode_Renderer_Exception( 0207 'Horizontal position must be greater than or equals 0' 0208 ); 0209 } 0210 $this->_leftOffset = intval($value); 0211 return $this; 0212 } 0213 0214 /** 0215 * Retrieve vertical adjustment 0216 * @return integer 0217 */ 0218 public function getLeftOffset() 0219 { 0220 return $this->_leftOffset; 0221 } 0222 0223 /** 0224 * Activate/Deactivate the automatic rendering of exception 0225 * 0226 * @param boolean $value 0227 * @return $this 0228 */ 0229 public function setAutomaticRenderError($value) 0230 { 0231 $this->_automaticRenderError = (bool) $value; 0232 return $this; 0233 } 0234 0235 /** 0236 * Horizontal position of the barcode in the rendering resource 0237 * 0238 * @param string $value 0239 * @return Zend_Barcode_Renderer 0240 * @throws Zend_Barcode_Renderer_Exception 0241 */ 0242 public function setHorizontalPosition($value) 0243 { 0244 if (!in_array($value, array('left' , 'center' , 'right'))) { 0245 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0246 throw new Zend_Barcode_Renderer_Exception( 0247 "Invalid barcode position provided must be 'left', 'center' or 'right'" 0248 ); 0249 } 0250 $this->_horizontalPosition = $value; 0251 return $this; 0252 } 0253 0254 /** 0255 * Horizontal position of the barcode in the rendering resource 0256 * @return string 0257 */ 0258 public function getHorizontalPosition() 0259 { 0260 return $this->_horizontalPosition; 0261 } 0262 0263 /** 0264 * Vertical position of the barcode in the rendering resource 0265 * 0266 * @param string $value 0267 * @return self 0268 * @throws Zend_Barcode_Renderer_Exception 0269 */ 0270 public function setVerticalPosition($value) 0271 { 0272 if (!in_array($value, array('top' , 'middle' , 'bottom'))) { 0273 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0274 throw new Zend_Barcode_Renderer_Exception( 0275 "Invalid barcode position provided must be 'top', 'middle' or 'bottom'" 0276 ); 0277 } 0278 $this->_verticalPosition = $value; 0279 return $this; 0280 } 0281 0282 /** 0283 * Vertical position of the barcode in the rendering resource 0284 * @return string 0285 */ 0286 public function getVerticalPosition() 0287 { 0288 return $this->_verticalPosition; 0289 } 0290 0291 /** 0292 * Set the size of a module 0293 * @param float $value 0294 * @return Zend_Barcode_Renderer 0295 * @throws Zend_Barcode_Renderer_Exception 0296 */ 0297 public function setModuleSize($value) 0298 { 0299 if (!is_numeric($value) || floatval($value) <= 0) { 0300 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0301 throw new Zend_Barcode_Renderer_Exception( 0302 'Float size must be greater than 0' 0303 ); 0304 } 0305 $this->_moduleSize = floatval($value); 0306 return $this; 0307 } 0308 0309 0310 /** 0311 * Set the size of a module 0312 * @return float 0313 */ 0314 public function getModuleSize() 0315 { 0316 return $this->_moduleSize; 0317 } 0318 0319 /** 0320 * Retrieve the automatic rendering of exception 0321 * @return boolean 0322 */ 0323 public function getAutomaticRenderError() 0324 { 0325 return $this->_automaticRenderError; 0326 } 0327 0328 /** 0329 * Set the barcode object 0330 * 0331 * @param Zend_Barcode_Object $barcode 0332 * @return Zend_Barcode_Renderer 0333 * @throws Zend_Barcode_Renderer_Exception 0334 */ 0335 public function setBarcode($barcode) 0336 { 0337 if (!$barcode instanceof Zend_Barcode_Object_ObjectAbstract) { 0338 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0339 throw new Zend_Barcode_Renderer_Exception( 0340 'Invalid barcode object provided to setBarcode()' 0341 ); 0342 } 0343 $this->_barcode = $barcode; 0344 return $this; 0345 } 0346 0347 /** 0348 * Retrieve the barcode object 0349 * @return Zend_Barcode_Object 0350 */ 0351 public function getBarcode() 0352 { 0353 return $this->_barcode; 0354 } 0355 0356 /** 0357 * Checking of parameters after all settings 0358 * @return boolean 0359 */ 0360 public function checkParams() 0361 { 0362 $this->_checkBarcodeObject(); 0363 $this->_checkParams(); 0364 return true; 0365 } 0366 0367 /** 0368 * Check if a barcode object is correctly provided 0369 * @return void 0370 * @throws Zend_Barcode_Renderer_Exception 0371 */ 0372 protected function _checkBarcodeObject() 0373 { 0374 if ($this->_barcode === null) { 0375 /** 0376 * @see Zend_Barcode_Renderer_Exception 0377 */ 0378 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0379 throw new Zend_Barcode_Renderer_Exception( 0380 'No barcode object provided' 0381 ); 0382 } 0383 } 0384 0385 /** 0386 * Calculate the left and top offset of the barcode in the 0387 * rendering support 0388 * 0389 * @param float $supportHeight 0390 * @param float $supportWidth 0391 * @return void 0392 */ 0393 protected function _adjustPosition($supportHeight, $supportWidth) 0394 { 0395 $barcodeHeight = $this->_barcode->getHeight(true) * $this->_moduleSize; 0396 if ($barcodeHeight != $supportHeight && $this->_topOffset == 0) { 0397 switch ($this->_verticalPosition) { 0398 case 'middle': 0399 $this->_topOffset = floor( 0400 ($supportHeight - $barcodeHeight) / 2); 0401 break; 0402 case 'bottom': 0403 $this->_topOffset = $supportHeight - $barcodeHeight; 0404 break; 0405 case 'top': 0406 default: 0407 $this->_topOffset = 0; 0408 break; 0409 } 0410 } 0411 $barcodeWidth = $this->_barcode->getWidth(true) * $this->_moduleSize; 0412 if ($barcodeWidth != $supportWidth && $this->_leftOffset == 0) { 0413 switch ($this->_horizontalPosition) { 0414 case 'center': 0415 $this->_leftOffset = floor( 0416 ($supportWidth - $barcodeWidth) / 2); 0417 break; 0418 case 'right': 0419 $this->_leftOffset = $supportWidth - $barcodeWidth; 0420 break; 0421 case 'left': 0422 default: 0423 $this->_leftOffset = 0; 0424 break; 0425 } 0426 } 0427 } 0428 0429 /** 0430 * Draw the barcode in the rendering resource 0431 * 0432 * @return mixed 0433 * @throws Zend_Exception 0434 * @throws Zend_Barcode_Exception 0435 */ 0436 public function draw() 0437 { 0438 try { 0439 $this->checkParams(); 0440 $this->_initRenderer(); 0441 $this->_drawInstructionList(); 0442 } catch (Zend_Exception $e) { 0443 $renderable = false; 0444 if ($e instanceof Zend_Barcode_Exception) { 0445 $renderable = $e->isRenderable(); 0446 } 0447 if ($this->_automaticRenderError && $renderable) { 0448 $barcode = Zend_Barcode::makeBarcode( 0449 'error', 0450 array('text' => $e->getMessage()) 0451 ); 0452 $this->setBarcode($barcode); 0453 $this->_resource = null; 0454 $this->_initRenderer(); 0455 $this->_drawInstructionList(); 0456 } else { 0457 if ($e instanceof Zend_Barcode_Exception) { 0458 $e->setIsRenderable(false); 0459 } 0460 throw $e; 0461 } 0462 } 0463 return $this->_resource; 0464 } 0465 0466 /** 0467 * Sub process to draw the barcode instructions 0468 * Needed by the automatic error rendering 0469 */ 0470 private function _drawInstructionList() 0471 { 0472 $instructionList = $this->_barcode->draw(); 0473 foreach ($instructionList as $instruction) { 0474 switch ($instruction['type']) { 0475 case 'polygon': 0476 $this->_drawPolygon( 0477 $instruction['points'], 0478 $instruction['color'], 0479 $instruction['filled'] 0480 ); 0481 break; 0482 case 'text': //$text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0) 0483 $this->_drawText( 0484 $instruction['text'], 0485 $instruction['size'], 0486 $instruction['position'], 0487 $instruction['font'], 0488 $instruction['color'], 0489 $instruction['alignment'], 0490 $instruction['orientation'] 0491 ); 0492 break; 0493 default: 0494 /** 0495 * @see Zend_Barcode_Renderer_Exception 0496 */ 0497 // require_once 'Zend/Barcode/Renderer/Exception.php'; 0498 throw new Zend_Barcode_Renderer_Exception( 0499 'Unknown drawing command' 0500 ); 0501 } 0502 } 0503 } 0504 0505 /** 0506 * Checking of parameters after all settings 0507 * @return void 0508 */ 0509 abstract protected function _checkParams(); 0510 0511 /** 0512 * Render the resource by sending headers and drawed resource 0513 * @return mixed 0514 */ 0515 abstract public function render(); 0516 0517 /** 0518 * Initialize the rendering resource 0519 * @return void 0520 */ 0521 abstract protected function _initRenderer(); 0522 0523 /** 0524 * Draw a polygon in the rendering resource 0525 * @param array $points 0526 * @param integer $color 0527 * @param boolean $filled 0528 */ 0529 abstract protected function _drawPolygon($points, $color, $filled = true); 0530 0531 /** 0532 * Draw a polygon in the rendering resource 0533 * 0534 * @param string $text 0535 * @param float $size 0536 * @param array $position 0537 * @param string $font 0538 * @param integer $color 0539 * @param string $alignment 0540 * @param float|int $orientation 0541 */ 0542 abstract protected function _drawText( 0543 $text, 0544 $size, 0545 $position, 0546 $font, 0547 $color, 0548 $alignment = 'center', 0549 $orientation = 0 0550 ); 0551 }