/vendor/dnyz520/careyshop-barcode/CodeItNow/BarcodeBundle/Generator/CINLabel.php

https://github.com/dnyz520/careyshop · PHP · 322 lines · 160 code · 35 blank · 127 comment · 22 complexity · 530962dd766037d171ced96e72e4684d MD5 · raw file

  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Class for Label
  6. *
  7. *--------------------------------------------------------------------
  8. * @author Akhtar Khan <er.akhtarkhan@gmail.com>
  9. * @link http://www.codeitnow.in
  10. * @package https://github.com/codeitnowin/barcode-generator
  11. */
  12. namespace CodeItNow\BarcodeBundle\Generator;
  13. use CodeItNow\BarcodeBundle\Generator\CINArgumentException;
  14. use CodeItNow\BarcodeBundle\Generator\CINFontPhp;
  15. use CodeItNow\BarcodeBundle\Generator\CINFontFile;
  16. class CINLabel {
  17. const POSITION_TOP = 0;
  18. const POSITION_RIGHT = 1;
  19. const POSITION_BOTTOM = 2;
  20. const POSITION_LEFT = 3;
  21. const ALIGN_LEFT = 0;
  22. const ALIGN_TOP = 0;
  23. const ALIGN_CENTER = 1;
  24. const ALIGN_RIGHT = 2;
  25. const ALIGN_BOTTOM = 2;
  26. private $font;
  27. private $text;
  28. private $position;
  29. private $alignment;
  30. private $offset;
  31. private $spacing;
  32. private $rotationAngle;
  33. private $backgroundColor;
  34. private $foregroundColor;
  35. /**
  36. * Constructor.
  37. *
  38. * @param string $text
  39. * @param CINFont $font
  40. * @param int $position
  41. * @param int $alignment
  42. */
  43. public function __construct($text = '', $font = null, $position = self::POSITION_BOTTOM, $alignment = self::ALIGN_CENTER) {
  44. $font = $font === null ? new CINFontPhp(5) : $font;
  45. $this->setFont($font);
  46. $this->setText($text);
  47. $this->setPosition($position);
  48. $this->setAlignment($alignment);
  49. $this->setSpacing(4);
  50. $this->setOffset(0);
  51. $this->setRotationAngle(0);
  52. $this->setBackgroundColor(new CINColor('white'));
  53. $this->setForegroundColor(new CINColor('black'));
  54. }
  55. /**
  56. * Gets the text.
  57. *
  58. * @return string
  59. */
  60. public function getText() {
  61. return $this->font->getText();
  62. }
  63. /**
  64. * Sets the text.
  65. *
  66. * @param string $text
  67. */
  68. public function setText($text) {
  69. $this->text = $text;
  70. $this->font->setText($this->text);
  71. }
  72. /**
  73. * Gets the font.
  74. *
  75. * @return CINFont
  76. */
  77. public function getFont() {
  78. return $this->font;
  79. }
  80. /**
  81. * Sets the font.
  82. *
  83. * @param CINFont $font
  84. */
  85. public function setFont($font) {
  86. if ($font === null) {
  87. throw new CINArgumentException('Font cannot be null.', 'font');
  88. }
  89. $this->font = clone $font;
  90. $this->font->setText($this->text);
  91. $this->font->setRotationAngle($this->rotationAngle);
  92. $this->font->setBackgroundColor($this->backgroundColor);
  93. $this->font->setForegroundColor($this->foregroundColor);
  94. }
  95. /**
  96. * Gets the text position for drawing.
  97. *
  98. * @return int
  99. */
  100. public function getPosition() {
  101. return $this->position;
  102. }
  103. /**
  104. * Sets the text position for drawing.
  105. *
  106. * @param int $position
  107. */
  108. public function setPosition($position) {
  109. $position = intval($position);
  110. if ($position !== self::POSITION_TOP && $position !== self::POSITION_RIGHT && $position !== self::POSITION_BOTTOM && $position !== self::POSITION_LEFT) {
  111. throw new CINArgumentException('The text position must be one of a valid constant.', 'position');
  112. }
  113. $this->position = $position;
  114. }
  115. /**
  116. * Gets the text alignment for drawing.
  117. *
  118. * @return int
  119. */
  120. public function getAlignment() {
  121. return $this->alignment;
  122. }
  123. /**
  124. * Sets the text alignment for drawing.
  125. *
  126. * @param int $alignment
  127. */
  128. public function setAlignment($alignment) {
  129. $alignment = intval($alignment);
  130. if ($alignment !== self::ALIGN_LEFT && $alignment !== self::ALIGN_TOP && $alignment !== self::ALIGN_CENTER && $alignment !== self::ALIGN_RIGHT && $alignment !== self::ALIGN_BOTTOM) {
  131. throw new CINArgumentException('The text alignment must be one of a valid constant.', 'alignment');
  132. }
  133. $this->alignment = $alignment;
  134. }
  135. /**
  136. * Gets the offset.
  137. *
  138. * @return int
  139. */
  140. public function getOffset() {
  141. return $this->offset;
  142. }
  143. /**
  144. * Sets the offset.
  145. *
  146. * @param int $offset
  147. */
  148. public function setOffset($offset) {
  149. $this->offset = intval($offset);
  150. }
  151. /**
  152. * Gets the spacing.
  153. *
  154. * @return int
  155. */
  156. public function getSpacing() {
  157. return $this->spacing;
  158. }
  159. /**
  160. * Sets the spacing.
  161. *
  162. * @param int $spacing
  163. */
  164. public function setSpacing($spacing) {
  165. $this->spacing = max(0, intval($spacing));
  166. }
  167. /**
  168. * Gets the rotation angle in degree.
  169. *
  170. * @return int
  171. */
  172. public function getRotationAngle() {
  173. return $this->font->getRotationAngle();
  174. }
  175. /**
  176. * Sets the rotation angle in degree.
  177. *
  178. * @param int $rotationAngle
  179. */
  180. public function setRotationAngle($rotationAngle) {
  181. $this->rotationAngle = intval($rotationAngle);
  182. $this->font->setRotationAngle($this->rotationAngle);
  183. }
  184. /**
  185. * Gets the background color in case of rotation.
  186. *
  187. * @return CINColor
  188. */
  189. public function getBackgroundColor() {
  190. return $this->backgroundColor;
  191. }
  192. /**
  193. * Sets the background color in case of rotation.
  194. *
  195. * @param CINColor $backgroundColor
  196. */
  197. public /*internal*/ function setBackgroundColor($backgroundColor) {
  198. $this->backgroundColor = $backgroundColor;
  199. $this->font->setBackgroundColor($this->backgroundColor);
  200. }
  201. /**
  202. * Gets the foreground color.
  203. *
  204. * @return CINColor
  205. */
  206. public function getForegroundColor() {
  207. return $this->font->getForegroundColor();
  208. }
  209. /**
  210. * Sets the foreground color.
  211. *
  212. * @param CINColor $foregroundColor
  213. */
  214. public function setForegroundColor($foregroundColor) {
  215. $this->foregroundColor = $foregroundColor;
  216. $this->font->setForegroundColor($this->foregroundColor);
  217. }
  218. /**
  219. * Gets the dimension taken by the label, including the spacing and offset.
  220. * [0]: width
  221. * [1]: height
  222. *
  223. * @return int[]
  224. */
  225. public function getDimension() {
  226. $w = 0;
  227. $h = 0;
  228. $dimension = $this->font->getDimension();
  229. $w = $dimension[0];
  230. $h = $dimension[1];
  231. if ($this->position === self::POSITION_TOP || $this->position === self::POSITION_BOTTOM) {
  232. $h += $this->spacing;
  233. $w += max(0, $this->offset);
  234. } else {
  235. $w += $this->spacing;
  236. $h += max(0, $this->offset);
  237. }
  238. return array($w, $h);
  239. }
  240. /**
  241. * Draws the text.
  242. * The coordinate passed are the positions of the barcode.
  243. * $x1 and $y1 represent the top left corner.
  244. * $x2 and $y2 represent the bottom right corner.
  245. *
  246. * @param resource $im
  247. * @param int $x1
  248. * @param int $y1
  249. * @param int $x2
  250. * @param int $y2
  251. */
  252. public /*internal*/ function draw($im, $x1, $y1, $x2, $y2) {
  253. $x = 0;
  254. $y = 0;
  255. $fontDimension = $this->font->getDimension();
  256. if ($this->position === self::POSITION_TOP || $this->position === self::POSITION_BOTTOM) {
  257. if ($this->position === self::POSITION_TOP) {
  258. $y = $y1 - $this->spacing - $fontDimension[1];
  259. } elseif ($this->position === self::POSITION_BOTTOM) {
  260. $y = $y2 + $this->spacing;
  261. }
  262. if ($this->alignment === self::ALIGN_CENTER) {
  263. $x = ($x2 - $x1) / 2 + $x1 - $fontDimension[0] / 2 + $this->offset;
  264. } elseif ($this->alignment === self::ALIGN_LEFT) {
  265. $x = $x1 + $this->offset;
  266. } else {
  267. $x = $x2 + $this->offset - $fontDimension[0];
  268. }
  269. } else {
  270. if ($this->position === self::POSITION_LEFT) {
  271. $x = $x1 - $this->spacing - $fontDimension[0];
  272. } elseif ($this->position === self::POSITION_RIGHT) {
  273. $x = $x2 + $this->spacing;
  274. }
  275. if ($this->alignment === self::ALIGN_CENTER) {
  276. $y = ($y2 - $y1) / 2 + $y1 - $fontDimension[1] / 2 + $this->offset;
  277. } elseif ($this->alignment === self::ALIGN_TOP) {
  278. $y = $y1 + $this->offset;
  279. } else {
  280. $y = $y2 + $this->offset - $fontDimension[1];
  281. }
  282. }
  283. $this->font->setText($this->text);
  284. $this->font->draw($im, $x, $y);
  285. }
  286. }
  287. ?>