ObjectAbstract.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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 Object
  18. * @copyright Copyright (c) 2005-2014 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 generate Barcode
  24. *
  25. * @category Zend
  26. * @package Zend_Barcode
  27. * @copyright Copyright (c) 2005-2014 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_Object_ObjectAbstract
  31. {
  32. /**
  33. * Namespace of the barcode for autoloading
  34. * @var string
  35. */
  36. protected $_barcodeNamespace = 'Zend_Barcode_Object';
  37. /**
  38. * Set of drawing instructions
  39. * @var array
  40. */
  41. protected $_instructions = array();
  42. /**
  43. * Barcode type
  44. * @var string
  45. */
  46. protected $_type = null;
  47. /**
  48. * Height of the object
  49. * @var integer
  50. */
  51. protected $_height = null;
  52. /**
  53. * Width of the object
  54. * @var integer
  55. */
  56. protected $_width = null;
  57. /**
  58. * Height of the bar
  59. * @var integer
  60. */
  61. protected $_barHeight = 50;
  62. /**
  63. * Width of a thin bar
  64. * @var integer
  65. */
  66. protected $_barThinWidth = 1;
  67. /**
  68. * Width of a thick bar
  69. * @var integer
  70. */
  71. protected $_barThickWidth = 3;
  72. /**
  73. * Factor to multiply bar and font measure
  74. * (barHeight, barThinWidth, barThickWidth & fontSize)
  75. * @var integer
  76. */
  77. protected $_factor = 1;
  78. /**
  79. * Font and bars color of the object
  80. * @var integer
  81. */
  82. protected $_foreColor = 0x000000;
  83. /**
  84. * Background color of the object
  85. * @var integer
  86. */
  87. protected $_backgroundColor = 0xFFFFFF;
  88. /**
  89. * Activate/deactivate border of the object
  90. * @var boolean
  91. */
  92. protected $_withBorder = false;
  93. /**
  94. * Activate/deactivate drawing of quiet zones
  95. * @var boolean
  96. */
  97. protected $_withQuietZones = true;
  98. /**
  99. * Force quiet zones even if
  100. * @var boolean
  101. */
  102. protected $_mandatoryQuietZones = false;
  103. /**
  104. * Orientation of the barcode in degrees
  105. * @var float
  106. */
  107. protected $_orientation = 0;
  108. /**
  109. * Offset from the top the object
  110. * (calculated from the orientation)
  111. * @var integer
  112. */
  113. protected $_offsetTop = null;
  114. /**
  115. * Offset from the left the object
  116. * (calculated from the orientation)
  117. * @var integer
  118. */
  119. protected $_offsetLeft = null;
  120. /**
  121. * Text to display
  122. * @var string
  123. */
  124. protected $_text = null;
  125. /**
  126. * Display (or not) human readable text
  127. * @var boolean
  128. */
  129. protected $_drawText = true;
  130. /**
  131. * Adjust (or not) position of human readable characters with barcode
  132. * @var boolean
  133. */
  134. protected $_stretchText = false;
  135. /**
  136. * Font resource
  137. * - integer (1 to 5): corresponds to GD included fonts
  138. * - string: corresponds to path of a TTF font
  139. * @var integer|string
  140. */
  141. protected $_font = null;
  142. /**
  143. * Font size
  144. * @var float
  145. */
  146. protected $_fontSize = 10;
  147. /**
  148. * Drawing of checksum
  149. * @var boolean
  150. */
  151. protected $_withChecksum = false;
  152. /**
  153. * Drawing of checksum inside text
  154. * @var boolean
  155. */
  156. protected $_withChecksumInText = false;
  157. /**
  158. * Fix barcode length (numeric or string like 'even')
  159. * @var $_barcodeLength integer | string
  160. */
  161. protected $_barcodeLength = null;
  162. /**
  163. * Activate automatic addition of leading zeros
  164. * if barcode length is fixed
  165. * @var $_addLeadingZeros boolean
  166. */
  167. protected $_addLeadingZeros = true;
  168. /**
  169. * Activation of mandatory checksum
  170. * to deactivate unauthorized modification
  171. * @var $_mandatoryChecksum boolean
  172. */
  173. protected $_mandatoryChecksum = false;
  174. /**
  175. * Character used to substitute checksum character for validation
  176. * @var $_substituteChecksumCharacter mixed
  177. */
  178. protected $_substituteChecksumCharacter = 0;
  179. /**
  180. * TTF font name: can be set before instanciation of the object
  181. * @var string
  182. */
  183. protected static $_staticFont = null;
  184. /**
  185. * Constructor
  186. *
  187. * @param array|Zend_Config $options
  188. */
  189. public function __construct($options = null)
  190. {
  191. $this->_getDefaultOptions();
  192. if (self::$_staticFont !== null) {
  193. $this->_font = self::$_staticFont;
  194. }
  195. if ($options instanceof Zend_Config) {
  196. $options = $options->toArray();
  197. }
  198. if (is_array($options)) {
  199. $this->setOptions($options);
  200. }
  201. $this->_type = strtolower(substr(get_class($this), strlen($this->_barcodeNamespace) + 1));
  202. if ($this->_mandatoryChecksum) {
  203. $this->_withChecksum = true;
  204. $this->_withChecksumInText = true;
  205. }
  206. }
  207. /**
  208. * Set default options for particular object
  209. * @return void
  210. */
  211. protected function _getDefaultOptions()
  212. {
  213. }
  214. /**
  215. * Set barcode state from options array
  216. * @param array $options
  217. * @return Zend_Barcode_Object
  218. */
  219. public function setOptions($options)
  220. {
  221. foreach ($options as $key => $value) {
  222. $method = 'set' . $key;
  223. if (method_exists($this, $method)) {
  224. $this->$method($value);
  225. }
  226. }
  227. return $this;
  228. }
  229. /**
  230. * Set barcode state from config object
  231. * @param Zend_Config $config
  232. * @return Zend_Barcode_Object
  233. */
  234. public function setConfig(Zend_Config $config)
  235. {
  236. return $this->setOptions($config->toArray());
  237. }
  238. /**
  239. * Set barcode namespace for autoloading
  240. *
  241. * @param string $namespace
  242. * @return Zend_Barcode_Object
  243. */
  244. public function setBarcodeNamespace($namespace)
  245. {
  246. $this->_barcodeNamespace = $namespace;
  247. return $this;
  248. }
  249. /**
  250. * Retrieve barcode namespace
  251. *
  252. * @return string
  253. */
  254. public function getBarcodeNamespace()
  255. {
  256. return $this->_barcodeNamespace;
  257. }
  258. /**
  259. * Retrieve type of barcode
  260. * @return string
  261. */
  262. public function getType()
  263. {
  264. return $this->_type;
  265. }
  266. /**
  267. * Set height of the barcode bar
  268. * @param integer $value
  269. * @return Zend_Barcode_Object
  270. * @throws Zend_Barcode_Object_Exception
  271. */
  272. public function setBarHeight($value)
  273. {
  274. if (intval($value) <= 0) {
  275. require_once 'Zend/Barcode/Object/Exception.php';
  276. throw new Zend_Barcode_Object_Exception(
  277. 'Bar height must be greater than 0'
  278. );
  279. }
  280. $this->_barHeight = intval($value);
  281. return $this;
  282. }
  283. /**
  284. * Get height of the barcode bar
  285. * @return integer
  286. */
  287. public function getBarHeight()
  288. {
  289. return $this->_barHeight;
  290. }
  291. /**
  292. * Set thickness of thin bar
  293. * @param integer $value
  294. * @return Zend_Barcode_Object
  295. * @throws Zend_Barcode_Object_Exception
  296. */
  297. public function setBarThinWidth($value)
  298. {
  299. if (intval($value) <= 0) {
  300. require_once 'Zend/Barcode/Object/Exception.php';
  301. throw new Zend_Barcode_Object_Exception(
  302. 'Bar width must be greater than 0'
  303. );
  304. }
  305. $this->_barThinWidth = intval($value);
  306. return $this;
  307. }
  308. /**
  309. * Get thickness of thin bar
  310. * @return integer
  311. */
  312. public function getBarThinWidth()
  313. {
  314. return $this->_barThinWidth;
  315. }
  316. /**
  317. * Set thickness of thick bar
  318. * @param integer $value
  319. * @return Zend_Barcode_Object
  320. * @throws Zend_Barcode_Object_Exception
  321. */
  322. public function setBarThickWidth($value)
  323. {
  324. if (intval($value) <= 0) {
  325. require_once 'Zend/Barcode/Object/Exception.php';
  326. throw new Zend_Barcode_Object_Exception(
  327. 'Bar width must be greater than 0'
  328. );
  329. }
  330. $this->_barThickWidth = intval($value);
  331. return $this;
  332. }
  333. /**
  334. * Get thickness of thick bar
  335. * @return integer
  336. */
  337. public function getBarThickWidth()
  338. {
  339. return $this->_barThickWidth;
  340. }
  341. /**
  342. * Set factor applying to
  343. * thinBarWidth - thickBarWidth - barHeight - fontSize
  344. * @param float $value
  345. * @return Zend_Barcode_Object
  346. * @throws Zend_Barcode_Object_Exception
  347. */
  348. public function setFactor($value)
  349. {
  350. if (floatval($value) <= 0) {
  351. require_once 'Zend/Barcode/Object/Exception.php';
  352. throw new Zend_Barcode_Object_Exception(
  353. 'Factor must be greater than 0'
  354. );
  355. }
  356. $this->_factor = floatval($value);
  357. return $this;
  358. }
  359. /**
  360. * Get factor applying to
  361. * thinBarWidth - thickBarWidth - barHeight - fontSize
  362. * @return integer
  363. */
  364. public function getFactor()
  365. {
  366. return $this->_factor;
  367. }
  368. /**
  369. * Set color of the barcode and text
  370. * @param string $value
  371. * @return Zend_Barcode_Object
  372. * @throws Zend_Barcode_Object_Exception
  373. */
  374. public function setForeColor($value)
  375. {
  376. if (preg_match('`\#[0-9A-F]{6}`', $value)) {
  377. $this->_foreColor = hexdec($value);
  378. } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
  379. $this->_foreColor = intval($value);
  380. } else {
  381. require_once 'Zend/Barcode/Object/Exception.php';
  382. throw new Zend_Barcode_Object_Exception(
  383. 'Text color must be set as #[0-9A-F]{6}'
  384. );
  385. }
  386. return $this;
  387. }
  388. /**
  389. * Retrieve color of the barcode and text
  390. * @return unknown
  391. */
  392. public function getForeColor()
  393. {
  394. return $this->_foreColor;
  395. }
  396. /**
  397. * Set the color of the background
  398. * @param integer $value
  399. * @return Zend_Barcode_Object
  400. * @throws Zend_Barcode_Object_Exception
  401. */
  402. public function setBackgroundColor($value)
  403. {
  404. if (preg_match('`\#[0-9A-F]{6}`', $value)) {
  405. $this->_backgroundColor = hexdec($value);
  406. } elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
  407. $this->_backgroundColor = intval($value);
  408. } else {
  409. require_once 'Zend/Barcode/Object/Exception.php';
  410. throw new Zend_Barcode_Object_Exception(
  411. 'Background color must be set as #[0-9A-F]{6}'
  412. );
  413. }
  414. return $this;
  415. }
  416. /**
  417. * Retrieve background color of the image
  418. * @return integer
  419. */
  420. public function getBackgroundColor()
  421. {
  422. return $this->_backgroundColor;
  423. }
  424. /**
  425. * Activate/deactivate drawing of the bar
  426. * @param boolean $value
  427. * @return Zend_Barcode_Object
  428. */
  429. public function setWithBorder($value)
  430. {
  431. $this->_withBorder = (bool) $value;
  432. return $this;
  433. }
  434. /**
  435. * Retrieve if border are draw or not
  436. * @return boolean
  437. */
  438. public function getWithBorder()
  439. {
  440. return $this->_withBorder;
  441. }
  442. /**
  443. * Activate/deactivate drawing of the quiet zones
  444. * @param boolean $value
  445. * @return Zend_Barcode_Object
  446. */
  447. public function setWithQuietZones($value)
  448. {
  449. $this->_withQuietZones = (bool) $value;
  450. return $this;
  451. }
  452. /**
  453. * Retrieve if quiet zones are draw or not
  454. * @return boolean
  455. */
  456. public function getWithQuietZones()
  457. {
  458. return $this->_withQuietZones;
  459. }
  460. /**
  461. * Allow fast inversion of font/bars color and background color
  462. * @return Zend_Barcode_Object
  463. */
  464. public function setReverseColor()
  465. {
  466. $tmp = $this->_foreColor;
  467. $this->_foreColor = $this->_backgroundColor;
  468. $this->_backgroundColor = $tmp;
  469. return $this;
  470. }
  471. /**
  472. * Set orientation of barcode and text
  473. * @param float $value
  474. * @return Zend_Barcode_Object
  475. * @throws Zend_Barcode_Object_Exception
  476. */
  477. public function setOrientation($value)
  478. {
  479. $this->_orientation = floatval($value) - floor(floatval($value) / 360) * 360;
  480. return $this;
  481. }
  482. /**
  483. * Retrieve orientation of barcode and text
  484. * @return float
  485. */
  486. public function getOrientation()
  487. {
  488. return $this->_orientation;
  489. }
  490. /**
  491. * Set text to encode
  492. * @param string $value
  493. * @return Zend_Barcode_Object
  494. */
  495. public function setText($value)
  496. {
  497. $this->_text = trim($value);
  498. return $this;
  499. }
  500. /**
  501. * Retrieve text to encode
  502. * @return string
  503. */
  504. public function getText()
  505. {
  506. $text = $this->_text;
  507. if ($this->_withChecksum) {
  508. $text .= $this->getChecksum($this->_text);
  509. }
  510. return $this->_addLeadingZeros($text);
  511. }
  512. /**
  513. * Automatically add leading zeros if barcode length is fixed
  514. *
  515. * @param string $text
  516. * @param boolean $withoutChecksum
  517. * @return string
  518. */
  519. protected function _addLeadingZeros($text, $withoutChecksum = false)
  520. {
  521. if ($this->_barcodeLength && $this->_addLeadingZeros) {
  522. $omitChecksum = (int) ($this->_withChecksum && $withoutChecksum);
  523. if (is_int($this->_barcodeLength)) {
  524. $length = $this->_barcodeLength - $omitChecksum;
  525. if (strlen($text) < $length) {
  526. $text = str_repeat('0', $length - strlen($text)) . $text;
  527. }
  528. } else {
  529. if ($this->_barcodeLength == 'even') {
  530. $text = ((strlen($text) - $omitChecksum) % 2 ? '0' . $text : $text);
  531. }
  532. }
  533. }
  534. return $text;
  535. }
  536. /**
  537. * Retrieve text to encode
  538. * @return string
  539. */
  540. public function getRawText()
  541. {
  542. return $this->_text;
  543. }
  544. /**
  545. * Retrieve text to display
  546. * @return string
  547. */
  548. public function getTextToDisplay()
  549. {
  550. if ($this->_withChecksumInText) {
  551. return $this->getText();
  552. } else {
  553. return $this->_addLeadingZeros($this->_text, true);
  554. }
  555. }
  556. /**
  557. * Activate/deactivate drawing of text to encode
  558. * @param boolean $value
  559. * @return Zend_Barcode_Object
  560. */
  561. public function setDrawText($value)
  562. {
  563. $this->_drawText = (bool) $value;
  564. return $this;
  565. }
  566. /**
  567. * Retrieve if drawing of text to encode is enabled
  568. * @return boolean
  569. */
  570. public function getDrawText()
  571. {
  572. return $this->_drawText;
  573. }
  574. /**
  575. * Activate/deactivate the adjustment of the position
  576. * of the characters to the position of the bars
  577. * @param boolean $value
  578. * @return Zend_Barcode_Object
  579. * @throws Zend_Barcode_Object_Exception
  580. */
  581. public function setStretchText($value)
  582. {
  583. $this->_stretchText = (bool) $value;
  584. return $this;
  585. }
  586. /**
  587. * Retrieve if the adjustment of the position of the characters
  588. * to the position of the bars is enabled
  589. * @return boolean
  590. */
  591. public function getStretchText()
  592. {
  593. return $this->_stretchText;
  594. }
  595. /**
  596. * Activate/deactivate the automatic generation
  597. * of the checksum character
  598. * added to the barcode text
  599. * @param boolean $value
  600. * @return Zend_Barcode_Object
  601. */
  602. public function setWithChecksum($value)
  603. {
  604. if (!$this->_mandatoryChecksum) {
  605. $this->_withChecksum = (bool) $value;
  606. }
  607. return $this;
  608. }
  609. /**
  610. * Retrieve if the checksum character is automatically
  611. * added to the barcode text
  612. * @return boolean
  613. */
  614. public function getWithChecksum()
  615. {
  616. return $this->_withChecksum;
  617. }
  618. /**
  619. * Activate/deactivate the automatic generation
  620. * of the checksum character
  621. * added to the barcode text
  622. * @param boolean $value
  623. * @return Zend_Barcode_Object
  624. * @throws Zend_Barcode_Object_Exception
  625. */
  626. public function setWithChecksumInText($value)
  627. {
  628. if (!$this->_mandatoryChecksum) {
  629. $this->_withChecksumInText = (bool) $value;
  630. }
  631. return $this;
  632. }
  633. /**
  634. * Retrieve if the checksum character is automatically
  635. * added to the barcode text
  636. * @return boolean
  637. */
  638. public function getWithChecksumInText()
  639. {
  640. return $this->_withChecksumInText;
  641. }
  642. /**
  643. * Set the font for all instances of barcode
  644. * @param string $font
  645. * @return void
  646. */
  647. public static function setBarcodeFont($font)
  648. {
  649. if (is_string($font) || (is_int($font) && $font >= 1 && $font <= 5)) {
  650. self::$_staticFont = $font;
  651. }
  652. }
  653. /**
  654. * Set the font:
  655. * - if integer between 1 and 5, use gd built-in fonts
  656. * - if string, $value is assumed to be the path to a TTF font
  657. * @param integer|string $value
  658. * @return Zend_Barcode_Object
  659. * @throws Zend_Barcode_Object_Exception
  660. */
  661. public function setFont($value)
  662. {
  663. if (is_int($value) && $value >= 1 && $value <= 5) {
  664. if (!extension_loaded('gd')) {
  665. require_once 'Zend/Barcode/Object/Exception.php';
  666. throw new Zend_Barcode_Object_Exception(
  667. 'GD extension is required to use numeric font'
  668. );
  669. }
  670. // Case of numeric font with GD
  671. $this->_font = $value;
  672. // In this case font size is given by:
  673. $this->_fontSize = imagefontheight($value);
  674. } elseif (is_string($value)) {
  675. $this->_font = $value;
  676. } else {
  677. require_once 'Zend/Barcode/Object/Exception.php';
  678. throw new Zend_Barcode_Object_Exception(sprintf(
  679. 'Invalid font "%s" provided to setFont()',
  680. $value
  681. ));
  682. }
  683. return $this;
  684. }
  685. /**
  686. * Retrieve the font
  687. * @return integer|string
  688. */
  689. public function getFont()
  690. {
  691. return $this->_font;
  692. }
  693. /**
  694. * Set the size of the font in case of TTF
  695. * @param float $value
  696. * @return Zend_Barcode_Object
  697. * @throws Zend_Barcode_Object_Exception
  698. */
  699. public function setFontSize($value)
  700. {
  701. if (is_numeric($this->_font)) {
  702. // Case of numeric font with GD
  703. return $this;
  704. }
  705. if (!is_numeric($value)) {
  706. require_once 'Zend/Barcode/Object/Exception.php';
  707. throw new Zend_Barcode_Object_Exception(
  708. 'Font size must be a numeric value'
  709. );
  710. }
  711. $this->_fontSize = $value;
  712. return $this;
  713. }
  714. /**
  715. * Retrieve the size of the font in case of TTF
  716. * @return float
  717. */
  718. public function getFontSize()
  719. {
  720. return $this->_fontSize;
  721. }
  722. /**
  723. * Quiet zone before first bar
  724. * and after the last bar
  725. * @return integer
  726. */
  727. public function getQuietZone()
  728. {
  729. if ($this->_withQuietZones || $this->_mandatoryQuietZones) {
  730. return 10 * $this->_barThinWidth * $this->_factor;
  731. } else {
  732. return 0;
  733. }
  734. }
  735. /**
  736. * Add an instruction in the array of instructions
  737. * @param array $instruction
  738. */
  739. protected function _addInstruction(array $instruction)
  740. {
  741. $this->_instructions[] = $instruction;
  742. }
  743. /**
  744. * Retrieve the set of drawing instructions
  745. * @return array
  746. */
  747. public function getInstructions()
  748. {
  749. return $this->_instructions;
  750. }
  751. /**
  752. * Add a polygon drawing instruction in the set of instructions
  753. * @param array $points
  754. * @param integer $color
  755. * @param boolean $filled
  756. */
  757. protected function _addPolygon(array $points, $color = null, $filled = true)
  758. {
  759. if ($color === null) {
  760. $color = $this->_foreColor;
  761. }
  762. $this->_addInstruction(array(
  763. 'type' => 'polygon',
  764. 'points' => $points,
  765. 'color' => $color,
  766. 'filled' => $filled,
  767. ));
  768. }
  769. /**
  770. * Add a text drawing instruction in the set of instructions
  771. *
  772. * @param string $text
  773. * @param float $size
  774. * @param array $position
  775. * @param string $font
  776. * @param integer $color
  777. * @param string $alignment
  778. * @param float|int $orientation
  779. */
  780. protected function _addText(
  781. $text,
  782. $size,
  783. $position,
  784. $font,
  785. $color,
  786. $alignment = 'center',
  787. $orientation = 0
  788. ) {
  789. if ($color === null) {
  790. $color = $this->_foreColor;
  791. }
  792. $this->_addInstruction(array(
  793. 'type' => 'text',
  794. 'text' => $text,
  795. 'size' => $size,
  796. 'position' => $position,
  797. 'font' => $font,
  798. 'color' => $color,
  799. 'alignment' => $alignment,
  800. 'orientation' => $orientation,
  801. ));
  802. }
  803. /**
  804. * Checking of parameters after all settings
  805. *
  806. * @return bool
  807. */
  808. public function checkParams()
  809. {
  810. $this->_checkText();
  811. $this->_checkFontAndOrientation();
  812. $this->_checkParams();
  813. return true;
  814. }
  815. /**
  816. * Check if a text is really provided to barcode
  817. *
  818. * @param string|null $value
  819. * @return void
  820. * @throws Zend_Barcode_Object_Exception
  821. */
  822. protected function _checkText($value = null)
  823. {
  824. if ($value === null) {
  825. $value = $this->_text;
  826. }
  827. if (!strlen($value)) {
  828. require_once 'Zend/Barcode/Object/Exception.php';
  829. throw new Zend_Barcode_Object_Exception(
  830. 'A text must be provide to Barcode before drawing'
  831. );
  832. }
  833. $this->validateText($value);
  834. }
  835. /**
  836. * Check the ratio between the thick and the thin bar
  837. *
  838. * @param int $min
  839. * @param int $max
  840. * @return void
  841. * @throws Zend_Barcode_Object_Exception
  842. */
  843. protected function _checkRatio($min = 2, $max = 3)
  844. {
  845. $ratio = $this->_barThickWidth / $this->_barThinWidth;
  846. if (!($ratio >= $min && $ratio <= $max)) {
  847. require_once 'Zend/Barcode/Object/Exception.php';
  848. throw new Zend_Barcode_Object_Exception(sprintf(
  849. 'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)',
  850. $min,
  851. $max,
  852. $ratio
  853. ));
  854. }
  855. }
  856. /**
  857. * Drawing with an angle is just allow TTF font
  858. *
  859. * @return void
  860. * @throws Zend_Barcode_Object_Exception
  861. */
  862. protected function _checkFontAndOrientation()
  863. {
  864. if (is_numeric($this->_font) && $this->_orientation != 0) {
  865. require_once 'Zend/Barcode/Object/Exception.php';
  866. throw new Zend_Barcode_Object_Exception(
  867. 'Only drawing with TTF font allow orientation of the barcode.'
  868. );
  869. }
  870. }
  871. /**
  872. * Width of the result image (before any rotation)
  873. *
  874. * @return integer
  875. */
  876. protected function _calculateWidth()
  877. {
  878. return (int) $this->_withBorder
  879. + $this->_calculateBarcodeWidth()
  880. + (int) $this->_withBorder;
  881. }
  882. /**
  883. * Calculate the width of the barcode
  884. *
  885. * @return integer
  886. */
  887. abstract protected function _calculateBarcodeWidth();
  888. /**
  889. * Height of the result object
  890. *
  891. * @return int
  892. */
  893. protected function _calculateHeight()
  894. {
  895. return (int) $this->_withBorder * 2
  896. + $this->_calculateBarcodeHeight()
  897. + (int) $this->_withBorder * 2;
  898. }
  899. /**
  900. * Height of the barcode
  901. *
  902. * @return int
  903. */
  904. protected function _calculateBarcodeHeight()
  905. {
  906. $textHeight = 0;
  907. $extraHeight = 0;
  908. if ($this->_drawText) {
  909. $textHeight += $this->_fontSize;
  910. $extraHeight = 2;
  911. }
  912. return ($this->_barHeight + $textHeight) * $this->_factor + $extraHeight;
  913. }
  914. /**
  915. * Get height of the result object
  916. *
  917. * @param bool $recalculate
  918. * @return int
  919. */
  920. public function getHeight($recalculate = false)
  921. {
  922. if ($this->_height === null || $recalculate) {
  923. $this->_height =
  924. abs($this->_calculateHeight() * cos($this->_orientation / 180 * pi()))
  925. + abs($this->_calculateWidth() * sin($this->_orientation / 180 * pi()));
  926. }
  927. return $this->_height;
  928. }
  929. /**
  930. * Get width of the result object
  931. *
  932. * @param bool $recalculate
  933. * @return int
  934. */
  935. public function getWidth($recalculate = false)
  936. {
  937. if ($this->_width === null || $recalculate) {
  938. $this->_width =
  939. abs($this->_calculateWidth() * cos($this->_orientation / 180 * pi()))
  940. + abs($this->_calculateHeight() * sin($this->_orientation / 180 * pi()));
  941. }
  942. return $this->_width;
  943. }
  944. /**
  945. * Calculate the offset from the left of the object
  946. * if an orientation is activated
  947. *
  948. * @param bool $recalculate
  949. * @return float
  950. */
  951. public function getOffsetLeft($recalculate = false)
  952. {
  953. if ($this->_offsetLeft === null || $recalculate) {
  954. $this->_offsetLeft = - min(array(
  955. 0 * cos(
  956. $this->_orientation / 180 * pi()) - 0 * sin(
  957. $this->_orientation / 180 * pi()),
  958. 0 * cos(
  959. $this->_orientation / 180 * pi()) - $this->_calculateBarcodeHeight() * sin(
  960. $this->_orientation / 180 * pi()),
  961. $this->_calculateBarcodeWidth() * cos(
  962. $this->_orientation / 180 * pi()) - $this->_calculateBarcodeHeight() * sin(
  963. $this->_orientation / 180 * pi()),
  964. $this->_calculateBarcodeWidth() * cos(
  965. $this->_orientation / 180 * pi()) - 0 * sin(
  966. $this->_orientation / 180 * pi()),
  967. ));
  968. }
  969. return $this->_offsetLeft;
  970. }
  971. /**
  972. * Calculate the offset from the top of the object
  973. * if an orientation is activated
  974. *
  975. * @param bool $recalculate
  976. * @return float
  977. */
  978. public function getOffsetTop($recalculate = false)
  979. {
  980. if ($this->_offsetTop === null || $recalculate) {
  981. $this->_offsetTop = - min(array(
  982. 0 * cos(
  983. $this->_orientation / 180 * pi()) + 0 * sin(
  984. $this->_orientation / 180 * pi()),
  985. $this->_calculateBarcodeHeight() * cos(
  986. $this->_orientation / 180 * pi()) + 0 * sin(
  987. $this->_orientation / 180 * pi()),
  988. $this->_calculateBarcodeHeight() * cos(
  989. $this->_orientation / 180 * pi()) + $this->_calculateBarcodeWidth() * sin(
  990. $this->_orientation / 180 * pi()),
  991. 0 * cos(
  992. $this->_orientation / 180 * pi()) + $this->_calculateBarcodeWidth() * sin(
  993. $this->_orientation / 180 * pi()),
  994. ));
  995. }
  996. return $this->_offsetTop;
  997. }
  998. /**
  999. * Apply rotation on a point in X/Y dimensions
  1000. *
  1001. * @param float $x1 x-position before rotation
  1002. * @param float $y1 y-position before rotation
  1003. * @return array Array of two elements corresponding to the new XY point
  1004. */
  1005. protected function _rotate($x1, $y1)
  1006. {
  1007. $x2 = $x1 * cos($this->_orientation / 180 * pi())
  1008. - $y1 * sin($this->_orientation / 180 * pi())
  1009. + $this->getOffsetLeft();
  1010. $y2 = $y1 * cos($this->_orientation / 180 * pi())
  1011. + $x1 * sin($this->_orientation / 180 * pi())
  1012. + $this->getOffsetTop();
  1013. return array(intval($x2) , intval($y2));
  1014. }
  1015. /**
  1016. * Complete drawing of the barcode
  1017. *
  1018. * @return array Table of instructions
  1019. */
  1020. public function draw()
  1021. {
  1022. $this->checkParams();
  1023. $this->_drawBarcode();
  1024. $this->_drawBorder();
  1025. $this->_drawText();
  1026. return $this->getInstructions();
  1027. }
  1028. /**
  1029. * Draw the barcode
  1030. *
  1031. * @return void
  1032. */
  1033. protected function _drawBarcode()
  1034. {
  1035. $barcodeTable = $this->_prepareBarcode();
  1036. $this->_preDrawBarcode();
  1037. $xpos = (int) $this->_withBorder;
  1038. $ypos = (int) $this->_withBorder;
  1039. $point1 = $this->_rotate(0, 0);
  1040. $point2 = $this->_rotate(0, $this->_calculateHeight() - 1);
  1041. $point3 = $this->_rotate(
  1042. $this->_calculateWidth() - 1,
  1043. $this->_calculateHeight() - 1
  1044. );
  1045. $point4 = $this->_rotate($this->_calculateWidth() - 1, 0);
  1046. $this->_addPolygon(array(
  1047. $point1,
  1048. $point2,
  1049. $point3,
  1050. $point4
  1051. ), $this->_backgroundColor);
  1052. $xpos += $this->getQuietZone();
  1053. $barLength = $this->_barHeight * $this->_factor;
  1054. foreach ($barcodeTable as $bar) {
  1055. $width = $bar[1] * $this->_factor;
  1056. if ($bar[0]) {
  1057. $point1 = $this->_rotate($xpos, $ypos + $bar[2] * $barLength);
  1058. $point2 = $this->_rotate($xpos, $ypos + $bar[3] * $barLength);
  1059. $point3 = $this->_rotate(
  1060. $xpos + $width - 1,
  1061. $ypos + $bar[3] * $barLength
  1062. );
  1063. $point4 = $this->_rotate(
  1064. $xpos + $width - 1,
  1065. $ypos + $bar[2] * $barLength
  1066. );
  1067. $this->_addPolygon(array(
  1068. $point1,
  1069. $point2,
  1070. $point3,
  1071. $point4,
  1072. ));
  1073. }
  1074. $xpos += $width;
  1075. }
  1076. $this->_postDrawBarcode();
  1077. }
  1078. /**
  1079. * Partial function to draw border
  1080. *
  1081. * @return void
  1082. */
  1083. protected function _drawBorder()
  1084. {
  1085. if ($this->_withBorder) {
  1086. $point1 = $this->_rotate(0, 0);
  1087. $point2 = $this->_rotate($this->_calculateWidth() - 1, 0);
  1088. $point3 = $this->_rotate(
  1089. $this->_calculateWidth() - 1,
  1090. $this->_calculateHeight() - 1
  1091. );
  1092. $point4 = $this->_rotate(0, $this->_calculateHeight() - 1);
  1093. $this->_addPolygon(array(
  1094. $point1,
  1095. $point2,
  1096. $point3,
  1097. $point4,
  1098. $point1,
  1099. ), $this->_foreColor, false);
  1100. }
  1101. }
  1102. /**
  1103. * Partial function to draw text
  1104. * @return void
  1105. */
  1106. protected function _drawText()
  1107. {
  1108. if ($this->_drawText) {
  1109. $text = $this->getTextToDisplay();
  1110. if ($this->_stretchText) {
  1111. $textLength = strlen($text);
  1112. $space = ($this->_calculateWidth() - 2 * $this->getQuietZone()) / $textLength;
  1113. for ($i = 0; $i < $textLength; $i ++) {
  1114. $leftPosition = $this->getQuietZone() + $space * ($i + 0.5);
  1115. $this->_addText(
  1116. $text{$i},
  1117. $this->_fontSize * $this->_factor,
  1118. $this->_rotate(
  1119. $leftPosition,
  1120. (int) $this->_withBorder * 2
  1121. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  1122. ),
  1123. $this->_font,
  1124. $this->_foreColor,
  1125. 'center',
  1126. - $this->_orientation
  1127. );
  1128. }
  1129. } else {
  1130. $this->_addText(
  1131. $text,
  1132. $this->_fontSize * $this->_factor,
  1133. $this->_rotate(
  1134. $this->_calculateWidth() / 2,
  1135. (int) $this->_withBorder * 2
  1136. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  1137. ),
  1138. $this->_font,
  1139. $this->_foreColor,
  1140. 'center',
  1141. - $this->_orientation
  1142. );
  1143. }
  1144. }
  1145. }
  1146. /**
  1147. * Check for invalid characters
  1148. * @param string $value Text to be ckecked
  1149. * @return void
  1150. */
  1151. public function validateText($value)
  1152. {
  1153. $this->_validateText($value);
  1154. }
  1155. /**
  1156. * Standard validation for most of barcode objects
  1157. *
  1158. * @param string $value
  1159. * @param array $options
  1160. * @throws Zend_Barcode_Object_Exception
  1161. */
  1162. protected function _validateText($value, $options = array())
  1163. {
  1164. $validatorName = (isset($options['validator'])) ? $options['validator'] : $this->getType();
  1165. $validator = new Zend_Validate_Barcode(array(
  1166. 'adapter' => $validatorName,
  1167. 'checksum' => false,
  1168. ));
  1169. $checksumCharacter = '';
  1170. $withChecksum = false;
  1171. if ($this->_mandatoryChecksum) {
  1172. $checksumCharacter = $this->_substituteChecksumCharacter;
  1173. $withChecksum = true;
  1174. }
  1175. $value = $this->_addLeadingZeros($value, $withChecksum) . $checksumCharacter;
  1176. if (!$validator->isValid($value)) {
  1177. $message = implode("\n", $validator->getMessages());
  1178. /**
  1179. * @see Zend_Barcode_Object_Exception
  1180. */
  1181. require_once 'Zend/Barcode/Object/Exception.php';
  1182. throw new Zend_Barcode_Object_Exception($message);
  1183. }
  1184. }
  1185. /**
  1186. * Each child must prepare the barcode and return
  1187. * a table like array(
  1188. * 0 => array(
  1189. * 0 => int (visible(black) or not(white))
  1190. * 1 => int (width of the bar)
  1191. * 2 => float (0->1 position from the top of the beginning of the bar in %)
  1192. * 3 => float (0->1 position from the top of the end of the bar in %)
  1193. * ),
  1194. * 1 => ...
  1195. * )
  1196. *
  1197. * @return array
  1198. */
  1199. abstract protected function _prepareBarcode();
  1200. /**
  1201. * Checking of parameters after all settings
  1202. *
  1203. * @return void
  1204. */
  1205. abstract protected function _checkParams();
  1206. /**
  1207. * Allow each child to draw something else
  1208. *
  1209. * @return void
  1210. */
  1211. protected function _preDrawBarcode()
  1212. {
  1213. }
  1214. /**
  1215. * Allow each child to draw something else
  1216. * (ex: bearer bars in interleaved 2 of 5 code)
  1217. *
  1218. * @return void
  1219. */
  1220. protected function _postDrawBarcode()
  1221. {
  1222. }
  1223. }