2 /* Libchart - PHP chart library
3 * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
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.
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.
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/>.
23 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
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;
34 * Creates a new text drawing helper.
36 public function Text() {
37 $baseDir = dirname(__FILE__) . "/../../../";
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";
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
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;
60 if (!($align & $this->VERTICAL_CENTER_ALIGN) && !($align & $this->VERTICAL_BOTTOM_ALIGN)) {
61 $align |= $this->VERTICAL_TOP_ALIGN;
67 list ($llx, $lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = imageftbbox($fontSize, 0, $fontFileName, $text, array("linespacing" => $lineSpacing));
69 $textWidth = $lrx - $llx;
70 $textHeight = $lry - $ury;
74 if ($align & $this->HORIZONTAL_CENTER_ALIGN) {
75 $px -= $textWidth / 2;
78 if ($align & $this->HORIZONTAL_RIGHT_ALIGN) {
82 if ($align & $this->VERTICAL_CENTER_ALIGN) {
83 $py += $textHeight / 2;
86 if ($align & $this->VERTICAL_TOP_ALIGN) {
90 imagettftext($img, $fontSize, $angle, $px, $py, $color->getColor($img), $fontFileName, $text);
94 * Print text centered horizontally on the image.
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
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);
107 * Print text in diagonal.
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
115 public function printDiagonal($img, $px, $py, $color, $text) {
117 $fontFileName = $this->fontCondensed;
121 list ($lx, $ly, $rx, $ry) = imageftbbox($fontSize, 0, $fontFileName, $text, array("linespacing" => $lineSpacing));
122 $textWidth = $rx - $lx;
126 imagettftext($img, $fontSize, $angle, $px, $py, $color->getColor($img), $fontFileName, $text);