BCGFontFile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Holds font family and size.
  6. *
  7. *--------------------------------------------------------------------
  8. * Copyright (C) Jean-Sebastien Goupil
  9. * http://www.barcodephp.com
  10. */
  11. include_once('BCGArgumentException.php');
  12. include_once('BCGFont.php');
  13. include_once('BCGColor.php');
  14. class BCGFontFile implements BCGFont {
  15. const PHP_BOX_FIX = 0;
  16. private $path;
  17. private $size;
  18. private $text = '';
  19. private $foregroundColor;
  20. private $rotationAngle;
  21. private $box;
  22. private $boxFix;
  23. /**
  24. * Constructor.
  25. *
  26. * @param string $fontPath path to the file
  27. * @param int $size size in point
  28. */
  29. public function __construct($fontPath, $size) {
  30. if (!file_exists($fontPath)) {
  31. throw new BCGArgumentException('The font path is incorrect.', 'fontPath');
  32. }
  33. $this->path = $fontPath;
  34. $this->size = $size;
  35. $this->foregroundColor = new BCGColor('black');
  36. $this->setRotationAngle(0);
  37. $this->setBoxFix(self::PHP_BOX_FIX);
  38. }
  39. /**
  40. * Gets the text associated to the font.
  41. *
  42. * @return string
  43. */
  44. public function getText() {
  45. return $this->text;
  46. }
  47. /**
  48. * Sets the text associated to the font.
  49. *
  50. * @param string text
  51. */
  52. public function setText($text) {
  53. $this->text = $text;
  54. $this->box = null;
  55. }
  56. /**
  57. * Gets the rotation in degree.
  58. *
  59. * @return int
  60. */
  61. public function getRotationAngle() {
  62. return (360 - $this->rotationAngle) % 360;
  63. }
  64. /**
  65. * Sets the rotation in degree.
  66. *
  67. * @param int
  68. */
  69. public function setRotationAngle($rotationAngle) {
  70. $this->rotationAngle = (int)$rotationAngle;
  71. if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
  72. $this->rotationAngle = 0;
  73. }
  74. $this->rotationAngle = (360 - $this->rotationAngle) % 360;
  75. $this->box = null;
  76. }
  77. /**
  78. * Gets the background color.
  79. *
  80. * @return BCGColor
  81. */
  82. public function getBackgroundColor() {
  83. }
  84. /**
  85. * Sets the background color.
  86. *
  87. * @param BCGColor $backgroundColor
  88. */
  89. public function setBackgroundColor($backgroundColor) {
  90. }
  91. /**
  92. * Gets the foreground color.
  93. *
  94. * @return BCGColor
  95. */
  96. public function getForegroundColor() {
  97. return $this->foregroundColor;
  98. }
  99. /**
  100. * Sets the foreground color.
  101. *
  102. * @param BCGColor $foregroundColor
  103. */
  104. public function setForegroundColor($foregroundColor) {
  105. $this->foregroundColor = $foregroundColor;
  106. }
  107. /**
  108. * Gets the box fix information.
  109. *
  110. * @return int
  111. */
  112. public function getBoxFix() {
  113. return $this->boxFix;
  114. }
  115. /**
  116. * Sets the box fix information.
  117. *
  118. * @param int $value
  119. */
  120. public function setBoxFix($value) {
  121. $this->boxFix = intval($value);
  122. }
  123. /**
  124. * Returns the width and height that the text takes to be written.
  125. *
  126. * @return int[]
  127. */
  128. public function getDimension() {
  129. $w = 0.0;
  130. $h = 0.0;
  131. $box = $this->getBox();
  132. if ($box !== null) {
  133. $minX = min(array($box[0], $box[2], $box[4], $box[6]));
  134. $maxX = max(array($box[0], $box[2], $box[4], $box[6]));
  135. $minY = min(array($box[1], $box[3], $box[5], $box[7]));
  136. $maxY = max(array($box[1], $box[3], $box[5], $box[7]));
  137. $w = $maxX - $minX;
  138. $h = $maxY - $minY;
  139. }
  140. $rotationAngle = $this->getRotationAngle();
  141. if ($rotationAngle === 90 || $rotationAngle === 270) {
  142. return array($h + self::PHP_BOX_FIX, $w);
  143. } else {
  144. return array($w + self::PHP_BOX_FIX, $h);
  145. }
  146. }
  147. /**
  148. * Draws the text on the image at a specific position.
  149. * $x and $y represent the left bottom corner.
  150. *
  151. * @param resource $im
  152. * @param int $x
  153. * @param int $y
  154. */
  155. public function draw($im, $x, $y) {
  156. $drawingPosition = $this->getDrawingPosition($x, $y);
  157. imagettftext($im, $this->size, $this->rotationAngle, $drawingPosition[0], $drawingPosition[1], $this->foregroundColor->allocate($im), $this->path, $this->text);
  158. }
  159. private function getDrawingPosition($x, $y) {
  160. $dimension = $this->getDimension();
  161. $box = $this->getBox();
  162. $rotationAngle = $this->getRotationAngle();
  163. if ($rotationAngle === 0) {
  164. $y += abs(min($box[5], $box[7]));
  165. } elseif ($rotationAngle === 90) {
  166. $x += abs(min($box[5], $box[7]));
  167. $y += $dimension[1];
  168. } elseif ($rotationAngle === 180) {
  169. $x += $dimension[0];
  170. $y += abs(max($box[1], $box[3]));
  171. } elseif ($rotationAngle === 270) {
  172. $x += abs(max($box[1], $box[3]));
  173. }
  174. return array($x, $y);
  175. }
  176. private function getBox() {
  177. if ($this->box === null) {
  178. $gd = imagecreate(1, 1);
  179. $this->box = imagettftext($gd, $this->size, 0, 0, 0, 0, $this->path, $this->text);
  180. }
  181. return $this->box;
  182. }
  183. }
  184. ?>