| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image; |
| 4 |
|
| 5 |
abstract class AbstractFont |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Text to be written |
| 9 |
* |
| 10 |
* @var String |
| 11 |
*/ |
| 12 |
public $text; |
| 13 |
|
| 14 |
/** |
| 15 |
* Text size in pixels |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
public $size = 12; |
| 20 |
|
| 21 |
/** |
| 22 |
* Color of the text |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
public $color = '000000'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Rotation angle of the text |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
public $angle = 0; |
| 34 |
|
| 35 |
/** |
| 36 |
* Horizontal alignment of the text |
| 37 |
* |
| 38 |
* @var String |
| 39 |
*/ |
| 40 |
public $align; |
| 41 |
|
| 42 |
/** |
| 43 |
* Vertical alignment of the text |
| 44 |
* |
| 45 |
* @var String |
| 46 |
*/ |
| 47 |
public $valign; |
| 48 |
|
| 49 |
/** |
| 50 |
* Space between text characters |
| 51 |
* |
| 52 |
* @var float |
| 53 |
*/ |
| 54 |
public $kerning = 0; |
| 55 |
|
| 56 |
/** |
| 57 |
* Path to TTF or GD library internal font file of the text |
| 58 |
* |
| 59 |
* @var mixed |
| 60 |
*/ |
| 61 |
public $file; |
| 62 |
|
| 63 |
/** |
| 64 |
* Draws font to given image on given position |
| 65 |
* |
| 66 |
* @param Image $image |
| 67 |
* @param int $posx |
| 68 |
* @param int $posy |
| 69 |
* @return boolean |
| 70 |
*/ |
| 71 |
abstract public function applyToImage(Image $image, $posx = 0, $posy = 0); |
| 72 |
|
| 73 |
/** |
| 74 |
* Calculates bounding box of current font setting |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
abstract public function getBoxSize(); |
| 79 |
|
| 80 |
/** |
| 81 |
* Create a new instance of Font |
| 82 |
* |
| 83 |
* @param String $text Text to be written |
| 84 |
*/ |
| 85 |
public function __construct($text = null) |
| 86 |
{ |
| 87 |
$this->text = $text; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Set text to be written |
| 92 |
* |
| 93 |
* @param String $text |
| 94 |
* @return self |
| 95 |
*/ |
| 96 |
public function text($text) |
| 97 |
{ |
| 98 |
$this->text = $text; |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get text to be written |
| 105 |
* |
| 106 |
* @return String |
| 107 |
*/ |
| 108 |
public function getText() |
| 109 |
{ |
| 110 |
return $this->text; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Set font size in pixels |
| 115 |
* |
| 116 |
* @param int $size |
| 117 |
* @return self |
| 118 |
*/ |
| 119 |
public function size($size) |
| 120 |
{ |
| 121 |
$this->size = $size; |
| 122 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get font size in pixels |
| 128 |
* |
| 129 |
* @return int |
| 130 |
*/ |
| 131 |
public function getSize() |
| 132 |
{ |
| 133 |
return $this->size; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Set color of text to be written |
| 138 |
* |
| 139 |
* @param mixed $color |
| 140 |
* @return self |
| 141 |
*/ |
| 142 |
public function color($color) |
| 143 |
{ |
| 144 |
$this->color = $color; |
| 145 |
|
| 146 |
return $this; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get color of text |
| 151 |
* |
| 152 |
* @return mixed |
| 153 |
*/ |
| 154 |
public function getColor() |
| 155 |
{ |
| 156 |
return $this->color; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Set rotation angle of text |
| 161 |
* |
| 162 |
* @param int $angle |
| 163 |
* @return self |
| 164 |
*/ |
| 165 |
public function angle($angle) |
| 166 |
{ |
| 167 |
$this->angle = $angle; |
| 168 |
|
| 169 |
return $this; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get rotation angle of text |
| 174 |
* |
| 175 |
* @return int |
| 176 |
*/ |
| 177 |
public function getAngle() |
| 178 |
{ |
| 179 |
return $this->angle; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Set horizontal text alignment |
| 184 |
* |
| 185 |
* @param string $align |
| 186 |
* @return self |
| 187 |
*/ |
| 188 |
public function align($align) |
| 189 |
{ |
| 190 |
$this->align = $align; |
| 191 |
|
| 192 |
return $this; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get horizontal text alignment |
| 197 |
* |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function getAlign() |
| 201 |
{ |
| 202 |
return $this->align; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Set vertical text alignment |
| 207 |
* |
| 208 |
* @param string $valign |
| 209 |
* @return self |
| 210 |
*/ |
| 211 |
public function valign($valign) |
| 212 |
{ |
| 213 |
$this->valign = $valign; |
| 214 |
|
| 215 |
return $this; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get vertical text alignment |
| 220 |
* |
| 221 |
* @return string |
| 222 |
*/ |
| 223 |
public function getValign() |
| 224 |
{ |
| 225 |
return $this->valign; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Set text kerning |
| 230 |
* |
| 231 |
* @param string $kerning |
| 232 |
* @return void |
| 233 |
*/ |
| 234 |
public function kerning($kerning) |
| 235 |
{ |
| 236 |
$this->kerning = $kerning; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get kerning |
| 241 |
* |
| 242 |
* @return float |
| 243 |
*/ |
| 244 |
public function getKerning() |
| 245 |
{ |
| 246 |
return $this->kerning; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Set path to font file |
| 251 |
* |
| 252 |
* @param string $file |
| 253 |
* @return self |
| 254 |
*/ |
| 255 |
public function file($file) |
| 256 |
{ |
| 257 |
$this->file = $file; |
| 258 |
|
| 259 |
return $this; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Get path to font file |
| 264 |
* |
| 265 |
* @return string |
| 266 |
*/ |
| 267 |
public function getFile() |
| 268 |
{ |
| 269 |
return $this->file; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Checks if current font has access to an applicable font file |
| 274 |
* |
| 275 |
* @return boolean |
| 276 |
*/ |
| 277 |
protected function hasApplicableFontFile() |
| 278 |
{ |
| 279 |
if (is_string($this->file)) { |
| 280 |
return file_exists($this->file); |
| 281 |
} |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Counts lines of text to be written |
| 288 |
* |
| 289 |
* @return int |
| 290 |
*/ |
| 291 |
public function countLines() |
| 292 |
{ |
| 293 |
return count(explode(PHP_EOL, $this->text)); |
| 294 |
} |
| 295 |
} |
| 296 |
|