AbstractFont.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 int
  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 int
  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 int $posx
  52. * @param int $posy
  53. * @return boolean
  54. */
  55. abstract public function applyToImage(Image $image, $posx = 0, $posy = 0);
  56. /**
  57. * Calculates bounding box of current font setting
  58. *
  59. * @return array
  60. */
  61. abstract public function getBoxSize();
  62. /**
  63. * Create a new instance of Font
  64. *
  65. * @param Strinf $text Text to be written
  66. */
  67. public function __construct($text = null)
  68. {
  69. $this->text = $text;
  70. }
  71. /**
  72. * Set text to be written
  73. *
  74. * @param String $text
  75. * @return void
  76. */
  77. public function text($text)
  78. {
  79. $this->text = $text;
  80. return $this;
  81. }
  82. /**
  83. * Get text to be written
  84. *
  85. * @return String
  86. */
  87. public function getText()
  88. {
  89. return $this->text;
  90. }
  91. /**
  92. * Set font size in pixels
  93. *
  94. * @param int $size
  95. * @return void
  96. */
  97. public function size($size)
  98. {
  99. $this->size = $size;
  100. return $this;
  101. }
  102. /**
  103. * Get font size in pixels
  104. *
  105. * @return int
  106. */
  107. public function getSize()
  108. {
  109. return $this->size;
  110. }
  111. /**
  112. * Set color of text to be written
  113. *
  114. * @param mixed $color
  115. * @return void
  116. */
  117. public function color($color)
  118. {
  119. $this->color = $color;
  120. return $this;
  121. }
  122. /**
  123. * Get color of text
  124. *
  125. * @return mixed
  126. */
  127. public function getColor()
  128. {
  129. return $this->color;
  130. }
  131. /**
  132. * Set rotation angle of text
  133. *
  134. * @param int $angle
  135. * @return void
  136. */
  137. public function angle($angle)
  138. {
  139. $this->angle = $angle;
  140. return $this;
  141. }
  142. /**
  143. * Get rotation angle of text
  144. *
  145. * @return int
  146. */
  147. public function getAngle()
  148. {
  149. return $this->angle;
  150. }
  151. /**
  152. * Set horizontal text alignment
  153. *
  154. * @param string $align
  155. * @return void
  156. */
  157. public function align($align)
  158. {
  159. $this->align = $align;
  160. return $this;
  161. }
  162. /**
  163. * Get horizontal text alignment
  164. *
  165. * @return string
  166. */
  167. public function getAlign()
  168. {
  169. return $this->align;
  170. }
  171. /**
  172. * Set vertical text alignment
  173. *
  174. * @param string $valign
  175. * @return void
  176. */
  177. public function valign($valign)
  178. {
  179. $this->valign = $valign;
  180. return $this;
  181. }
  182. /**
  183. * Get vertical text alignment
  184. *
  185. * @return string
  186. */
  187. public function getValign()
  188. {
  189. return $this->valign;
  190. }
  191. /**
  192. * Set path to font file
  193. *
  194. * @param string $file
  195. * @return void
  196. */
  197. public function file($file)
  198. {
  199. $this->file = $file;
  200. return $this;
  201. }
  202. /**
  203. * Get path to font file
  204. *
  205. * @return string
  206. */
  207. public function getFile()
  208. {
  209. return $this->file;
  210. }
  211. /**
  212. * Checks if current font has access to an applicable font file
  213. *
  214. * @return boolean
  215. */
  216. protected function hasApplicableFontFile()
  217. {
  218. if (is_string($this->file)) {
  219. return file_exists($this->file);
  220. }
  221. return false;
  222. }
  223. /**
  224. * Counts lines of text to be written
  225. *
  226. * @return int
  227. */
  228. public function countLines()
  229. {
  230. return count(explode(PHP_EOL, $this->text));
  231. }
  232. }