BCGBarcode1D.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Holds all type of barcodes for 1D generation
  6. *
  7. *--------------------------------------------------------------------
  8. * Copyright (C) Jean-Sebastien Goupil
  9. * http://www.barcodephp.com
  10. */
  11. include_once('BCGArgumentException.php');
  12. include_once('BCGBarcode.php');
  13. include_once('BCGFontPhp.php');
  14. include_once('BCGFontFile.php');
  15. include_once('BCGLabel.php');
  16. abstract class BCGBarcode1D extends BCGBarcode {
  17. const SIZE_SPACING_FONT = 5;
  18. const AUTO_LABEL = '##!!AUTO_LABEL!!##';
  19. protected $thickness; // int
  20. protected $keys, $code; // string[]
  21. protected $positionX; // int
  22. protected $font; // BCGFont
  23. protected $text; // string
  24. protected $checksumValue; // int or int[]
  25. protected $displayChecksum; // bool
  26. protected $label; // string
  27. protected $defaultLabel; // BCGLabel
  28. /**
  29. * Constructor.
  30. */
  31. protected function __construct() {
  32. parent::__construct();
  33. $this->setThickness(30);
  34. $this->defaultLabel = new BCGLabel();
  35. $this->defaultLabel->setPosition(BCGLabel::POSITION_BOTTOM);
  36. $this->setLabel(self::AUTO_LABEL);
  37. $this->setFont(new BCGFontPhp(5));
  38. $this->text = '';
  39. $this->checksumValue = false;
  40. $this->positionX = 0;
  41. }
  42. /**
  43. * Gets the thickness.
  44. *
  45. * @return int
  46. */
  47. public function getThickness() {
  48. return $this->thickness;
  49. }
  50. /**
  51. * Sets the thickness.
  52. *
  53. * @param int $thickness
  54. */
  55. public function setThickness($thickness) {
  56. $thickness = intval($thickness);
  57. if ($thickness <= 0) {
  58. throw new BCGArgumentException('The thickness must be larger than 0.', 'thickness');
  59. }
  60. $this->thickness = $thickness;
  61. }
  62. /**
  63. * Gets the label.
  64. * If the label was set to BCGBarcode1D::AUTO_LABEL, the label will display the value from the text parsed.
  65. *
  66. * @return string
  67. */
  68. public function getLabel() {
  69. $label = $this->label;
  70. if ($this->label === self::AUTO_LABEL) {
  71. $label = $this->text;
  72. if ($this->displayChecksum === true && ($checksum = $this->processChecksum()) !== false) {
  73. $label .= $checksum;
  74. }
  75. }
  76. return $label;
  77. }
  78. /**
  79. * Sets the label.
  80. * You can use BCGBarcode::AUTO_LABEL to have the label automatically written based on the parsed text.
  81. *
  82. * @param string $label
  83. */
  84. public function setLabel($label) {
  85. $this->label = $label;
  86. }
  87. /**
  88. * Gets the font.
  89. *
  90. * @return BCGFont
  91. */
  92. public function getFont() {
  93. return $this->font;
  94. }
  95. /**
  96. * Sets the font.
  97. *
  98. * @param mixed $font BCGFont or int
  99. */
  100. public function setFont($font) {
  101. if (is_int($font)) {
  102. if ($font === 0) {
  103. $font = null;
  104. } else {
  105. $font = new BCGFontPhp($font);
  106. }
  107. }
  108. $this->font = $font;
  109. }
  110. /**
  111. * Parses the text before displaying it.
  112. *
  113. * @param mixed $text
  114. */
  115. public function parse($text) {
  116. $this->text = $text;
  117. $this->checksumValue = false; // Reset checksumValue
  118. $this->validate();
  119. parent::parse($text);
  120. $this->addDefaultLabel();
  121. }
  122. /**
  123. * Gets the checksum of a Barcode.
  124. * If no checksum is available, return FALSE.
  125. *
  126. * @return string
  127. */
  128. public function getChecksum() {
  129. return $this->processChecksum();
  130. }
  131. /**
  132. * Sets if the checksum is displayed with the label or not.
  133. * The checksum must be activated in some case to make this variable effective.
  134. *
  135. * @param boolean $displayChecksum
  136. */
  137. public function setDisplayChecksum($displayChecksum) {
  138. $this->displayChecksum = (bool)$displayChecksum;
  139. }
  140. /**
  141. * Adds the default label.
  142. */
  143. protected function addDefaultLabel() {
  144. $label = $this->getLabel();
  145. $font = $this->font;
  146. if ($label !== null && $label !== '' && $font !== null && $this->defaultLabel !== null) {
  147. $this->defaultLabel->setText($label);
  148. $this->defaultLabel->setFont($font);
  149. $this->addLabel($this->defaultLabel);
  150. }
  151. }
  152. /**
  153. * Validates the input
  154. */
  155. protected function validate() {
  156. // No validation in the abstract class.
  157. }
  158. /**
  159. * Returns the index in $keys (useful for checksum).
  160. *
  161. * @param mixed $var
  162. * @return mixed
  163. */
  164. protected function findIndex($var) {
  165. return array_search($var, $this->keys);
  166. }
  167. /**
  168. * Returns the code of the char (useful for drawing bars).
  169. *
  170. * @param mixed $var
  171. * @return string
  172. */
  173. protected function findCode($var) {
  174. return $this->code[$this->findIndex($var)];
  175. }
  176. /**
  177. * Draws all chars thanks to $code. If $startBar is true, the line begins by a space.
  178. * If $startBar is false, the line begins by a bar.
  179. *
  180. * @param resource $im
  181. * @param string $code
  182. * @param boolean $startBar
  183. */
  184. protected function drawChar($im, $code, $startBar = true) {
  185. $colors = array(BCGBarcode::COLOR_FG, BCGBarcode::COLOR_BG);
  186. $currentColor = $startBar ? 0 : 1;
  187. $c = strlen($code);
  188. for ($i = 0; $i < $c; $i++) {
  189. for ($j = 0; $j < intval($code[$i]) + 1; $j++) {
  190. $this->drawSingleBar($im, $colors[$currentColor]);
  191. $this->nextX();
  192. }
  193. $currentColor = ($currentColor + 1) % 2;
  194. }
  195. }
  196. /**
  197. * Draws a Bar of $color depending of the resolution.
  198. *
  199. * @param resource $img
  200. * @param int $color
  201. */
  202. protected function drawSingleBar($im, $color) {
  203. $this->drawFilledRectangle($im, $this->positionX, 0, $this->positionX, $this->thickness - 1, $color);
  204. }
  205. /**
  206. * Moving the pointer right to write a bar.
  207. */
  208. protected function nextX() {
  209. $this->positionX++;
  210. }
  211. /**
  212. * Method that saves FALSE into the checksumValue. This means no checksum
  213. * but this method should be overriden when needed.
  214. */
  215. protected function calculateChecksum() {
  216. $this->checksumValue = false;
  217. }
  218. /**
  219. * Returns FALSE because there is no checksum. This method should be
  220. * overriden to return correctly the checksum in string with checksumValue.
  221. *
  222. * @return string
  223. */
  224. protected function processChecksum() {
  225. return false;
  226. }
  227. }
  228. ?>