PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Barcode/Renderer/AbstractRenderer.php

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