1 <?php
2 /**
3 * @author Gasper Kozak
4 * @copyright 2007-2011
5
6 This file is part of WideImage.
7
8 WideImage is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 WideImage is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with WideImage; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 * @package WideImage
23 **/
24
25 /**
26 * TTF font support class
27 *
28 * @package WideImage
29 */
30 class WideImage_Font_TTF
31 {
32 public $face;
33 public $size;
34 public $color;
35
36 function __construct($face, $size, $color)
37 {
38 $this->face = $face;
39 $this->size = $size;
40 $this->color = $color;
41 }
42
43 /**
44 * Writes text onto an image
45 *
46 * @param WideImage_Image $image
47 * @param mixed $x smart coordinate
48 * @param mixed $y smart coordinate
49 * @param string $text
50 * @param int $angle Angle in degrees clockwise
51 */
52 function writeText($image, $x, $y, $text, $angle = 0)
53 {
54 if ($image->isTrueColor())
55 $image->alphaBlending(true);
56
57 $box = imageftbbox($this->size, $angle, $this->face, $text);
58 $obox = array(
59 'left' => min($box[0], $box[2], $box[4], $box[6]),
60 'top' => min($box[1], $box[3], $box[5], $box[7]),
61 'right' => max($box[0], $box[2], $box[4], $box[6]) - 1,
62 'bottom' => max($box[1], $box[3], $box[5], $box[7]) - 1
63 );
64 $obox['width'] = abs($obox['left']) + abs($obox['right']);
65 $obox['height'] = abs($obox['top']) + abs($obox['bottom']);
66
67 $x = WideImage_Coordinate::fix($x, $image->getWidth(), $obox['width']);
68 $y = WideImage_Coordinate::fix($y, $image->getHeight(), $obox['height']);
69
70 $fixed_x = $x - $obox['left'];
71 $fixed_y = $y - $obox['top'];
72
73 imagettftext($image->getHandle(), $this->size, $angle, $fixed_x, $fixed_y, $this->color, $this->face, $text);
74 }
75 }
76