RendererAbstract.php 15 KB

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