Initial repo created
[timetracker.git] / WEB-INF / lib / libchart / classes / view / text / Text.php
1 <?php
2         /* Libchart - PHP chart library
3          * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
4          * 
5          * This program is free software: you can redistribute it and/or modify
6          * it under the terms of the GNU General Public License as published by
7          * the Free Software Foundation, either version 3 of the License, or
8          * (at your option) any later version.
9          * 
10          * This program is distributed in the hope that it will be useful,
11          * but WITHOUT ANY WARRANTY; without even the implied warranty of
12          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13          * GNU General Public License for more details.
14          *
15          * You should have received a copy of the GNU General Public License
16          * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17          * 
18          */
19         
20         /**
21          * Text drawing helper
22          *
23          * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
24          */
25         class Text {
26                 public $HORIZONTAL_LEFT_ALIGN = 1;
27                 public $HORIZONTAL_CENTER_ALIGN = 2;
28                 public $HORIZONTAL_RIGHT_ALIGN = 4;
29                 public $VERTICAL_TOP_ALIGN = 8;
30                 public $VERTICAL_CENTER_ALIGN = 16;
31                 public $VERTICAL_BOTTOM_ALIGN = 32;
32
33                 /**
34                  * Creates a new text drawing helper.
35                  */
36                 public function Text() {
37                         $baseDir = dirname(__FILE__) . "/../../../";
38                 
39                         // Free low-res fonts based on Bitstream Vera <http://dejavu.sourceforge.net/wiki/>
40                         $this->fontCondensed = $baseDir . "fonts/DejaVuSansCondensed.ttf";
41                         $this->fontCondensedBold = $baseDir . "fonts/DejaVuSansCondensed-Bold.ttf";
42                 }
43
44                 /**
45                  * Print text.
46                  *
47                  * @param Image GD image
48                  * @param integer text coordinate (x)
49                  * @param integer text coordinate (y)
50                  * @param Color text color
51                  * @param string text value
52                  * @param string font file name
53                  * @param bitfield text alignment
54                  */
55                 public function printText($img, $px, $py, $color, $text, $fontFileName, $align = 0) {
56                         if (!($align & $this->HORIZONTAL_CENTER_ALIGN) && !($align & $this->HORIZONTAL_RIGHT_ALIGN)) {
57                                 $align |= $this->HORIZONTAL_LEFT_ALIGN;
58                         }
59
60                         if (!($align & $this->VERTICAL_CENTER_ALIGN) && !($align & $this->VERTICAL_BOTTOM_ALIGN)) {
61                                 $align |= $this->VERTICAL_TOP_ALIGN;
62                         }
63
64                         $fontSize = 8;
65                         $lineSpacing = 1;
66
67                         list ($llx, $lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = imageftbbox($fontSize, 0, $fontFileName, $text, array("linespacing" => $lineSpacing));
68
69                         $textWidth = $lrx - $llx;
70                         $textHeight = $lry - $ury;
71
72                         $angle = 0;
73
74                         if ($align & $this->HORIZONTAL_CENTER_ALIGN) {
75                                 $px -= $textWidth / 2;
76                         }
77
78                         if ($align & $this->HORIZONTAL_RIGHT_ALIGN) {
79                                 $px -= $textWidth;
80                         }
81
82                         if ($align & $this->VERTICAL_CENTER_ALIGN) {
83                                 $py += $textHeight / 2;
84                         }
85
86                         if ($align & $this->VERTICAL_TOP_ALIGN) {
87                                 $py += $textHeight;
88                         }
89
90                         imagettftext($img, $fontSize, $angle, $px, $py, $color->getColor($img), $fontFileName, $text);
91                 }
92                 
93                 /**
94                  * Print text centered horizontally on the image.
95                  *
96                  * @param Image GD image
97                  * @param integer text coordinate (y)
98                  * @param Color text color
99                  * @param string text value
100                  * @param string font file name
101                  */
102                 public function printCentered($img, $py, $color, $text, $fontFileName) {
103                         $this->printText($img, imagesx($img) / 2, $py, $color, $text, $fontFileName, $this->HORIZONTAL_CENTER_ALIGN | $this->VERTICAL_CENTER_ALIGN);
104                 }
105
106                 /**
107                  * Print text in diagonal.
108                  *
109                  * @param Image GD image
110                  * @param integer text coordinate (x)
111                  * @param integer text coordinate (y)
112                  * @param Color text color
113                  * @param string text value
114                  */
115                 public function printDiagonal($img, $px, $py, $color, $text) {
116                         $fontSize = 8;
117                         $fontFileName = $this->fontCondensed;
118
119                         $lineSpacing = 1;
120
121                         list ($lx, $ly, $rx, $ry) = imageftbbox($fontSize, 0, $fontFileName, $text, array("linespacing" => $lineSpacing));
122                         $textWidth = $rx - $lx;
123
124                         $angle = -45;
125
126                         imagettftext($img, $fontSize, $angle, $px, $py, $color->getColor($img), $fontFileName, $text);
127                 }
128         }
129 ?>