BCGLabel.php 8.3 KB

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