AbstractFont.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Intervention\Image;
  3. abstract class AbstractFont
  4. {
  5. /**
  6. * Text to be written
  7. *
  8. * @var String
  9. */
  10. public $text;
  11. /**
  12. * Text size in pixels
  13. *
  14. * @var integer
  15. */
  16. public $size = 12;
  17. /**
  18. * Color of the text
  19. *
  20. * @var mixed
  21. */
  22. public $color = '000000';
  23. /**
  24. * Rotation angle of the text
  25. *
  26. * @var integer
  27. */
  28. public $angle = 0;
  29. /**
  30. * Horizontal alignment of the text
  31. *
  32. * @var String
  33. */
  34. public $align;
  35. /**
  36. * Vertical alignment of the text
  37. *
  38. * @var String
  39. */
  40. public $valign;
  41. /**
  42. * Path to TTF or GD library internal font file of the text
  43. *
  44. * @var mixed
  45. */
  46. public $file;
  47. /**
  48. * Draws font to given image on given position
  49. *
  50. * @param Image $image
  51. * @param integer $posx
  52. * @param integer $posy
  53. * @return boolean
  54. */
  55. abstract public function applyToImage(Image $image, $posx = 0, $posy = 0);
  56. /**
  57. * Create a new instance of Font
  58. *
  59. * @param Strinf $text Text to be written
  60. */
  61. public function __construct($text = null)
  62. {
  63. $this->text = $text;
  64. }
  65. /**
  66. * Set text to be written
  67. *
  68. * @param String $text
  69. * @return void
  70. */
  71. public function text($text)
  72. {
  73. $this->text = $text;
  74. }
  75. /**
  76. * Get text to be written
  77. *
  78. * @return String
  79. */
  80. public function getText()
  81. {
  82. return $this->text;
  83. }
  84. /**
  85. * Set font size in pixels
  86. *
  87. * @param integer $size
  88. * @return void
  89. */
  90. public function size($size)
  91. {
  92. $this->size = $size;
  93. }
  94. /**
  95. * Get font size in pixels
  96. *
  97. * @return integer
  98. */
  99. public function getSize()
  100. {
  101. return $this->size;
  102. }
  103. /**
  104. * Set color of text to be written
  105. *
  106. * @param mixed $color
  107. * @return void
  108. */
  109. public function color($color)
  110. {
  111. $this->color = $color;
  112. }
  113. /**
  114. * Get color of text
  115. *
  116. * @return mixed
  117. */
  118. public function getColor()
  119. {
  120. return $this->color;
  121. }
  122. /**
  123. * Set rotation angle of text
  124. *
  125. * @param integer $angle
  126. * @return void
  127. */
  128. public function angle($angle)
  129. {
  130. $this->angle = $angle;
  131. }
  132. /**
  133. * Get rotation angle of text
  134. *
  135. * @return integer
  136. */
  137. public function getAngle()
  138. {
  139. return $this->angle;
  140. }
  141. /**
  142. * Set horizontal text alignment
  143. *
  144. * @param string $align
  145. * @return void
  146. */
  147. public function align($align)
  148. {
  149. $this->align = $align;
  150. }
  151. /**
  152. * Get horizontal text alignment
  153. *
  154. * @return string
  155. */
  156. public function getAlign()
  157. {
  158. return $this->align;
  159. }
  160. /**
  161. * Set vertical text alignment
  162. *
  163. * @param string $valign
  164. * @return void
  165. */
  166. public function valign($valign)
  167. {
  168. $this->valign = $valign;
  169. }
  170. /**
  171. * Get vertical text alignment
  172. *
  173. * @return string
  174. */
  175. public function getValign()
  176. {
  177. return $this->valign;
  178. }
  179. /**
  180. * Set path to font file
  181. *
  182. * @param string $align
  183. * @return void
  184. */
  185. public function file($file)
  186. {
  187. $this->file = $file;
  188. }
  189. /**
  190. * Get path to font file
  191. *
  192. * @return string
  193. */
  194. public function getFile()
  195. {
  196. return $this->file;
  197. }
  198. /**
  199. * Checks if current font has access to an applicable font file
  200. *
  201. * @return boolean
  202. */
  203. protected function hasApplicableFontFile()
  204. {
  205. if (is_string($this->file)) {
  206. return file_exists($this->file);
  207. }
  208. return false;
  209. }
  210. /**
  211. * Counts lines of text to be written
  212. *
  213. * @return integer
  214. */
  215. public function countLines()
  216. {
  217. return count(explode(PHP_EOL, $this->text));
  218. }
  219. }