PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Barcode/Renderer/RendererAbstract.php

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