ObjectAbstract.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. * @param array|Zend_Config $options
  187. * @return void
  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. * @throw 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. * @throw 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. * @throw 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. * @throw 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. * @throw 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. * @throw 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. * @throw 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. * @param string $text
  515. * @param boolean $withoutChecksum
  516. */
  517. protected function _addLeadingZeros($text, $withoutChecksum = false)
  518. {
  519. if ($this->_barcodeLength && $this->_addLeadingZeros) {
  520. $omitChecksum = (int) ($this->_withChecksum && $withoutChecksum);
  521. if (is_int($this->_barcodeLength)) {
  522. $length = $this->_barcodeLength - $omitChecksum;
  523. if (strlen($text) < $length) {
  524. $text = str_repeat('0', $length - strlen($text)) . $text;
  525. }
  526. } else {
  527. if ($this->_barcodeLength == 'even') {
  528. $text = ((strlen($text) - $omitChecksum) % 2 ? '0' . $text : $text);
  529. }
  530. }
  531. }
  532. return $text;
  533. }
  534. /**
  535. * Retrieve text to encode
  536. * @return string
  537. */
  538. public function getRawText()
  539. {
  540. return $this->_text;
  541. }
  542. /**
  543. * Retrieve text to display
  544. * @return string
  545. */
  546. public function getTextToDisplay()
  547. {
  548. if ($this->_withChecksumInText) {
  549. return $this->getText();
  550. } else {
  551. return $this->_addLeadingZeros($this->_text, true);
  552. }
  553. }
  554. /**
  555. * Activate/deactivate drawing of text to encode
  556. * @param boolean $value
  557. * @return Zend_Barcode_Object
  558. */
  559. public function setDrawText($value)
  560. {
  561. $this->_drawText = (bool) $value;
  562. return $this;
  563. }
  564. /**
  565. * Retrieve if drawing of text to encode is enabled
  566. * @return boolean
  567. */
  568. public function getDrawText()
  569. {
  570. return $this->_drawText;
  571. }
  572. /**
  573. * Activate/deactivate the adjustment of the position
  574. * of the characters to the position of the bars
  575. * @param boolean $value
  576. * @return Zend_Barcode_Object
  577. * @throw Zend_Barcode_Object_Exception
  578. */
  579. public function setStretchText($value)
  580. {
  581. $this->_stretchText = (bool) $value;
  582. return $this;
  583. }
  584. /**
  585. * Retrieve if the adjustment of the position of the characters
  586. * to the position of the bars is enabled
  587. * @return boolean
  588. */
  589. public function getStretchText()
  590. {
  591. return $this->_stretchText;
  592. }
  593. /**
  594. * Activate/deactivate the automatic generation
  595. * of the checksum character
  596. * added to the barcode text
  597. * @param boolean $value
  598. * @return Zend_Barcode_Object
  599. */
  600. public function setWithChecksum($value)
  601. {
  602. if (!$this->_mandatoryChecksum) {
  603. $this->_withChecksum = (bool) $value;
  604. }
  605. return $this;
  606. }
  607. /**
  608. * Retrieve if the checksum character is automatically
  609. * added to the barcode text
  610. * @return boolean
  611. */
  612. public function getWithChecksum()
  613. {
  614. return $this->_withChecksum;
  615. }
  616. /**
  617. * Activate/deactivate the automatic generation
  618. * of the checksum character
  619. * added to the barcode text
  620. * @param boolean $value
  621. * @return Zend_Barcode_Object
  622. * @throw Zend_Barcode_Object_Exception
  623. */
  624. public function setWithChecksumInText($value)
  625. {
  626. if (!$this->_mandatoryChecksum) {
  627. $this->_withChecksumInText = (bool) $value;
  628. }
  629. return $this;
  630. }
  631. /**
  632. * Retrieve if the checksum character is automatically
  633. * added to the barcode text
  634. * @return boolean
  635. */
  636. public function getWithChecksumInText()
  637. {
  638. return $this->_withChecksumInText;
  639. }
  640. /**
  641. * Set the font for all instances of barcode
  642. * @param string $font
  643. * @return void
  644. */
  645. public static function setBarcodeFont($font)
  646. {
  647. if (is_string($font) || (is_int($font) && $font >= 1 && $font <= 5)) {
  648. self::$_staticFont = $font;
  649. }
  650. }
  651. /**
  652. * Set the font:
  653. * - if integer between 1 and 5, use gd built-in fonts
  654. * - if string, $value is assumed to be the path to a TTF font
  655. * @param integer|string $value
  656. * @return Zend_Barcode_Object
  657. * @throw Zend_Barcode_Object_Exception
  658. */
  659. public function setFont($value)
  660. {
  661. if (is_int($value) && $value >= 1 && $value <= 5) {
  662. if (!extension_loaded('gd')) {
  663. require_once 'Zend/Barcode/Object/Exception.php';
  664. throw new Zend_Barcode_Object_Exception(
  665. 'GD extension is required to use numeric font'
  666. );
  667. }
  668. // Case of numeric font with GD
  669. $this->_font = $value;
  670. // In this case font size is given by:
  671. $this->_fontSize = imagefontheight($value);
  672. } elseif (is_string($value)) {
  673. $this->_font = $value;
  674. } else {
  675. require_once 'Zend/Barcode/Object/Exception.php';
  676. throw new Zend_Barcode_Object_Exception(sprintf(
  677. 'Invalid font "%s" provided to setFont()',
  678. $value
  679. ));
  680. }
  681. return $this;
  682. }
  683. /**
  684. * Retrieve the font
  685. * @return integer|string
  686. */
  687. public function getFont()
  688. {
  689. return $this->_font;
  690. }
  691. /**
  692. * Set the size of the font in case of TTF
  693. * @param float $value
  694. * @return Zend_Barcode_Object
  695. * @throw Zend_Barcode_Object_Exception
  696. */
  697. public function setFontSize($value)
  698. {
  699. if (is_numeric($this->_font)) {
  700. // Case of numeric font with GD
  701. return $this;
  702. }
  703. if (!is_numeric($value)) {
  704. require_once 'Zend/Barcode/Object/Exception.php';
  705. throw new Zend_Barcode_Object_Exception(
  706. 'Font size must be a numeric value'
  707. );
  708. }
  709. $this->_fontSize = $value;
  710. return $this;
  711. }
  712. /**
  713. * Retrieve the size of the font in case of TTF
  714. * @return float
  715. */
  716. public function getFontSize()
  717. {
  718. return $this->_fontSize;
  719. }
  720. /**
  721. * Quiet zone before first bar
  722. * and after the last bar
  723. * @return integer
  724. */
  725. public function getQuietZone()
  726. {
  727. if ($this->_withQuietZones || $this->_mandatoryQuietZones) {
  728. return 10 * $this->_barThinWidth * $this->_factor;
  729. } else {
  730. return 0;
  731. }
  732. }
  733. /**
  734. * Add an instruction in the array of instructions
  735. * @param array $instruction
  736. */
  737. protected function _addInstruction(array $instruction)
  738. {
  739. $this->_instructions[] = $instruction;
  740. }
  741. /**
  742. * Retrieve the set of drawing instructions
  743. * @return array
  744. */
  745. public function getInstructions()
  746. {
  747. return $this->_instructions;
  748. }
  749. /**
  750. * Add a polygon drawing instruction in the set of instructions
  751. * @param array $points
  752. * @param integer $color
  753. * @param boolean $filled
  754. */
  755. protected function _addPolygon(array $points, $color = null, $filled = true)
  756. {
  757. if ($color === null) {
  758. $color = $this->_foreColor;
  759. }
  760. $this->_addInstruction(array(
  761. 'type' => 'polygon',
  762. 'points' => $points,
  763. 'color' => $color,
  764. 'filled' => $filled,
  765. ));
  766. }
  767. /**
  768. * Add a text drawing instruction in the set of instructions
  769. * @param string $text
  770. * @param float $size
  771. * @param array $position
  772. * @param string $font
  773. * @param integer $color
  774. * @param string $alignment
  775. * @param float $orientation
  776. */
  777. protected function _addText(
  778. $text,
  779. $size,
  780. $position,
  781. $font,
  782. $color,
  783. $alignment = 'center',
  784. $orientation = 0
  785. ) {
  786. if ($color === null) {
  787. $color = $this->_foreColor;
  788. }
  789. $this->_addInstruction(array(
  790. 'type' => 'text',
  791. 'text' => $text,
  792. 'size' => $size,
  793. 'position' => $position,
  794. 'font' => $font,
  795. 'color' => $color,
  796. 'alignment' => $alignment,
  797. 'orientation' => $orientation,
  798. ));
  799. }
  800. /**
  801. * Checking of parameters after all settings
  802. * @return void
  803. */
  804. public function checkParams()
  805. {
  806. $this->_checkText();
  807. $this->_checkFontAndOrientation();
  808. $this->_checkParams();
  809. return true;
  810. }
  811. /**
  812. * Check if a text is really provided to barcode
  813. * @return void
  814. * @throw Zend_Barcode_Object_Exception
  815. */
  816. protected function _checkText($value = null)
  817. {
  818. if ($value === null) {
  819. $value = $this->_text;
  820. }
  821. if (!strlen($value)) {
  822. require_once 'Zend/Barcode/Object/Exception.php';
  823. throw new Zend_Barcode_Object_Exception(
  824. 'A text must be provide to Barcode before drawing'
  825. );
  826. }
  827. $this->validateText($value);
  828. }
  829. /**
  830. * Check the ratio between the thick and the thin bar
  831. * @param integer $min
  832. * @param integer $max
  833. * @return void
  834. * @throw Zend_Barcode_Object_Exception
  835. */
  836. protected function _checkRatio($min = 2, $max = 3)
  837. {
  838. $ratio = $this->_barThickWidth / $this->_barThinWidth;
  839. if (!($ratio >= $min && $ratio <= $max)) {
  840. require_once 'Zend/Barcode/Object/Exception.php';
  841. throw new Zend_Barcode_Object_Exception(sprintf(
  842. 'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)',
  843. $min,
  844. $max,
  845. $ratio
  846. ));
  847. }
  848. }
  849. /**
  850. * Drawing with an angle is just allow TTF font
  851. * @return void
  852. * @throw Zend_Barcode_Object_Exception
  853. */
  854. protected function _checkFontAndOrientation()
  855. {
  856. if (is_numeric($this->_font) && $this->_orientation != 0) {
  857. require_once 'Zend/Barcode/Object/Exception.php';
  858. throw new Zend_Barcode_Object_Exception(
  859. 'Only drawing with TTF font allow orientation of the barcode.'
  860. );
  861. }
  862. }
  863. /**
  864. * Width of the result image
  865. * (before any rotation)
  866. * @return integer
  867. */
  868. protected function _calculateWidth()
  869. {
  870. return (int) $this->_withBorder
  871. + $this->_calculateBarcodeWidth()
  872. + (int) $this->_withBorder;
  873. }
  874. /**
  875. * Calculate the width of the barcode
  876. * @return integer
  877. */
  878. abstract protected function _calculateBarcodeWidth();
  879. /**
  880. * Height of the result object
  881. * @return integer
  882. */
  883. protected function _calculateHeight()
  884. {
  885. return (int) $this->_withBorder * 2
  886. + $this->_calculateBarcodeHeight()
  887. + (int) $this->_withBorder * 2;
  888. }
  889. /**
  890. * Height of the barcode
  891. * @return integer
  892. */
  893. protected function _calculateBarcodeHeight()
  894. {
  895. $textHeight = 0;
  896. $extraHeight = 0;
  897. if ($this->_drawText) {
  898. $textHeight += $this->_fontSize;
  899. $extraHeight = 2;
  900. }
  901. return ($this->_barHeight + $textHeight) * $this->_factor + $extraHeight;
  902. }
  903. /**
  904. * Get height of the result object
  905. * @return integer
  906. */
  907. public function getHeight($recalculate = false)
  908. {
  909. if ($this->_height === null || $recalculate) {
  910. $this->_height =
  911. abs($this->_calculateHeight() * cos($this->_orientation / 180 * pi()))
  912. + abs($this->_calculateWidth() * sin($this->_orientation / 180 * pi()));
  913. }
  914. return $this->_height;
  915. }
  916. /**
  917. * Get width of the result object
  918. * @return integer
  919. */
  920. public function getWidth($recalculate = false)
  921. {
  922. if ($this->_width === null || $recalculate) {
  923. $this->_width =
  924. abs($this->_calculateWidth() * cos($this->_orientation / 180 * pi()))
  925. + abs($this->_calculateHeight() * sin($this->_orientation / 180 * pi()));
  926. }
  927. return $this->_width;
  928. }
  929. /**
  930. * Calculate the offset from the left of the object
  931. * if an orientation is activated
  932. * @param boolean $recalculate
  933. * @return float
  934. */
  935. public function getOffsetLeft($recalculate = false)
  936. {
  937. if ($this->_offsetLeft === null || $recalculate) {
  938. $this->_offsetLeft = - min(array(
  939. 0 * cos(
  940. $this->_orientation / 180 * pi()) - 0 * sin(
  941. $this->_orientation / 180 * pi()),
  942. 0 * cos(
  943. $this->_orientation / 180 * pi()) - $this->_calculateBarcodeHeight() * sin(
  944. $this->_orientation / 180 * pi()),
  945. $this->_calculateBarcodeWidth() * cos(
  946. $this->_orientation / 180 * pi()) - $this->_calculateBarcodeHeight() * sin(
  947. $this->_orientation / 180 * pi()),
  948. $this->_calculateBarcodeWidth() * cos(
  949. $this->_orientation / 180 * pi()) - 0 * sin(
  950. $this->_orientation / 180 * pi()),
  951. ));
  952. }
  953. return $this->_offsetLeft;
  954. }
  955. /**
  956. * Calculate the offset from the top of the object
  957. * if an orientation is activated
  958. * @param boolean $recalculate
  959. * @return float
  960. */
  961. public function getOffsetTop($recalculate = false)
  962. {
  963. if ($this->_offsetTop === null || $recalculate) {
  964. $this->_offsetTop = - min(array(
  965. 0 * cos(
  966. $this->_orientation / 180 * pi()) + 0 * sin(
  967. $this->_orientation / 180 * pi()),
  968. $this->_calculateBarcodeHeight() * cos(
  969. $this->_orientation / 180 * pi()) + 0 * sin(
  970. $this->_orientation / 180 * pi()),
  971. $this->_calculateBarcodeHeight() * cos(
  972. $this->_orientation / 180 * pi()) + $this->_calculateBarcodeWidth() * sin(
  973. $this->_orientation / 180 * pi()),
  974. 0 * cos(
  975. $this->_orientation / 180 * pi()) + $this->_calculateBarcodeWidth() * sin(
  976. $this->_orientation / 180 * pi()),
  977. ));
  978. }
  979. return $this->_offsetTop;
  980. }
  981. /**
  982. * Apply rotation on a point in X/Y dimensions
  983. * @param float $x1 x-position before rotation
  984. * @param float $y1 y-position before rotation
  985. * @return array Array of two elements corresponding to the new XY point
  986. */
  987. protected function _rotate($x1, $y1)
  988. {
  989. $x2 = $x1 * cos($this->_orientation / 180 * pi())
  990. - $y1 * sin($this->_orientation / 180 * pi())
  991. + $this->getOffsetLeft();
  992. $y2 = $y1 * cos($this->_orientation / 180 * pi())
  993. + $x1 * sin($this->_orientation / 180 * pi())
  994. + $this->getOffsetTop();
  995. return array(intval($x2) , intval($y2));
  996. }
  997. /**
  998. * Complete drawing of the barcode
  999. * @return array Table of instructions
  1000. */
  1001. public function draw()
  1002. {
  1003. $this->checkParams();
  1004. $this->_drawBarcode();
  1005. $this->_drawBorder();
  1006. $this->_drawText();
  1007. return $this->getInstructions();
  1008. }
  1009. /**
  1010. * Draw the barcode
  1011. * @return void
  1012. */
  1013. protected function _drawBarcode()
  1014. {
  1015. $barcodeTable = $this->_prepareBarcode();
  1016. $this->_preDrawBarcode();
  1017. $xpos = (int) $this->_withBorder;
  1018. $ypos = (int) $this->_withBorder;
  1019. $point1 = $this->_rotate(0, 0);
  1020. $point2 = $this->_rotate(0, $this->_calculateHeight() - 1);
  1021. $point3 = $this->_rotate(
  1022. $this->_calculateWidth() - 1,
  1023. $this->_calculateHeight() - 1
  1024. );
  1025. $point4 = $this->_rotate($this->_calculateWidth() - 1, 0);
  1026. $this->_addPolygon(array(
  1027. $point1,
  1028. $point2,
  1029. $point3,
  1030. $point4
  1031. ), $this->_backgroundColor);
  1032. $xpos += $this->getQuietZone();
  1033. $barLength = $this->_barHeight * $this->_factor;
  1034. foreach ($barcodeTable as $bar) {
  1035. $width = $bar[1] * $this->_factor;
  1036. if ($bar[0]) {
  1037. $point1 = $this->_rotate($xpos, $ypos + $bar[2] * $barLength);
  1038. $point2 = $this->_rotate($xpos, $ypos + $bar[3] * $barLength);
  1039. $point3 = $this->_rotate(
  1040. $xpos + $width - 1,
  1041. $ypos + $bar[3] * $barLength
  1042. );
  1043. $point4 = $this->_rotate(
  1044. $xpos + $width - 1,
  1045. $ypos + $bar[2] * $barLength
  1046. );
  1047. $this->_addPolygon(array(
  1048. $point1,
  1049. $point2,
  1050. $point3,
  1051. $point4,
  1052. ));
  1053. }
  1054. $xpos += $width;
  1055. }
  1056. $this->_postDrawBarcode();
  1057. }
  1058. /**
  1059. * Partial function to draw border
  1060. * @return void
  1061. */
  1062. protected function _drawBorder()
  1063. {
  1064. if ($this->_withBorder) {
  1065. $point1 = $this->_rotate(0, 0);
  1066. $point2 = $this->_rotate($this->_calculateWidth() - 1, 0);
  1067. $point3 = $this->_rotate(
  1068. $this->_calculateWidth() - 1,
  1069. $this->_calculateHeight() - 1
  1070. );
  1071. $point4 = $this->_rotate(0, $this->_calculateHeight() - 1);
  1072. $this->_addPolygon(array(
  1073. $point1,
  1074. $point2,
  1075. $point3,
  1076. $point4,
  1077. $point1,
  1078. ), $this->_foreColor, false);
  1079. }
  1080. }
  1081. /**
  1082. * Partial function to draw text
  1083. * @return void
  1084. */
  1085. protected function _drawText()
  1086. {
  1087. if ($this->_drawText) {
  1088. $text = $this->getTextToDisplay();
  1089. if ($this->_stretchText) {
  1090. $textLength = strlen($text);
  1091. $space = ($this->_calculateWidth() - 2 * $this->getQuietZone()) / $textLength;
  1092. for ($i = 0; $i < $textLength; $i ++) {
  1093. $leftPosition = $this->getQuietZone() + $space * ($i + 0.5);
  1094. $this->_addText(
  1095. $text{$i},
  1096. $this->_fontSize * $this->_factor,
  1097. $this->_rotate(
  1098. $leftPosition,
  1099. (int) $this->_withBorder * 2
  1100. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  1101. ),
  1102. $this->_font,
  1103. $this->_foreColor,
  1104. 'center',
  1105. - $this->_orientation
  1106. );
  1107. }
  1108. } else {
  1109. $this->_addText(
  1110. $text,
  1111. $this->_fontSize * $this->_factor,
  1112. $this->_rotate(
  1113. $this->_calculateWidth() / 2,
  1114. (int) $this->_withBorder * 2
  1115. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  1116. ),
  1117. $this->_font,
  1118. $this->_foreColor,
  1119. 'center',
  1120. - $this->_orientation
  1121. );
  1122. }
  1123. }
  1124. }
  1125. /**
  1126. * Check for invalid characters
  1127. * @param string $value Text to be ckecked
  1128. * @return void
  1129. */
  1130. public function validateText($value)
  1131. {
  1132. $this->_validateText($value);
  1133. }
  1134. /**
  1135. * Standard validation for most of barcode objects
  1136. * @param string $value
  1137. * @param array $options
  1138. */
  1139. protected function _validateText($value, $options = array())
  1140. {
  1141. $validatorName = (isset($options['validator'])) ? $options['validator'] : $this->getType();
  1142. $validator = new Zend_Validate_Barcode(array(
  1143. 'adapter' => $validatorName,
  1144. 'checksum' => false,
  1145. ));
  1146. $checksumCharacter = '';
  1147. $withChecksum = false;
  1148. if ($this->_mandatoryChecksum) {
  1149. $checksumCharacter = $this->_substituteChecksumCharacter;
  1150. $withChecksum = true;
  1151. }
  1152. $value = $this->_addLeadingZeros($value, $withChecksum) . $checksumCharacter;
  1153. if (!$validator->isValid($value)) {
  1154. $message = implode("\n", $validator->getMessages());
  1155. /**
  1156. * @see Zend_Barcode_Object_Exception
  1157. */
  1158. require_once 'Zend/Barcode/Object/Exception.php';
  1159. throw new Zend_Barcode_Object_Exception($message);
  1160. }
  1161. }
  1162. /**
  1163. * Each child must prepare the barcode and return
  1164. * a table like array(
  1165. * 0 => array(
  1166. * 0 => int (visible(black) or not(white))
  1167. * 1 => int (width of the bar)
  1168. * 2 => float (0->1 position from the top of the beginning of the bar in %)
  1169. * 3 => float (0->1 position from the top of the end of the bar in %)
  1170. * ),
  1171. * 1 => ...
  1172. * )
  1173. *
  1174. * @return array
  1175. */
  1176. abstract protected function _prepareBarcode();
  1177. /**
  1178. * Checking of parameters after all settings
  1179. *
  1180. * @return void
  1181. */
  1182. abstract protected function _checkParams();
  1183. /**
  1184. * Allow each child to draw something else
  1185. *
  1186. * @return void
  1187. */
  1188. protected function _preDrawBarcode()
  1189. {
  1190. }
  1191. /**
  1192. * Allow each child to draw something else
  1193. * (ex: bearer bars in interleaved 2 of 5 code)
  1194. *
  1195. * @return void
  1196. */
  1197. protected function _postDrawBarcode()
  1198. {
  1199. }
  1200. }