cvs: pear /Image_Canvas/Canvas GD.php /Image_Canvas/docs ChangeLog README

From: Date: Wed, 22 Mar 2006 21:56:04 +0000
Subject: cvs: pear /Image_Canvas/Canvas GD.php /Image_Canvas/docs ChangeLog README
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-39253@lists.php.net to get a copy of this message
nosey Wed Mar 22 21:56:04 2006 UTC Modified files: /pear/Image_Canvas/docs ChangeLog README /pear/Image_Canvas/Canvas GD.php Log: Modified GD canvas addText() method with TTF fonts - (x, y) parameters now specify the absolute lower-left corner of the text (if angle is 0) instead of the baseline of the first letter, making texts appear incorrectly. This however require fonts to have a steady baseline, like fx. Arial, Verdana, etc. unlike Comic

http://cvs.php.net/viewcvs.cgi/pear/Image_Canvas/docs/ChangeLog?r1=1.20&r2=1.21&diff_format=u Index: pear/Image_Canvas/docs/ChangeLog diff -u pear/Image_Canvas/docs/ChangeLog:1.20 pear/Image_Canvas/docs/ChangeLog:1.21 --- pear/Image_Canvas/docs/ChangeLog:1.20 Thu Mar 2 12:42:01 2006 +++ pear/Image_Canvas/docs/ChangeLog Wed Mar 22 21:56:04 2006 @@ -1,7 +1,10 @@ ************************** Image_Canvas - Changelog ************************** -Version 0.3.0 [2005-Mar-02] - Jesper Veggerby <pear.nosey@veggerby.dk> +Version 0.3.1 [2006-xxx-xx] - Jesper Veggerby <pear.nosey@veggerby.dk> + * Modified GD canvas addText() method with TTF fonts - (x, y) parameters now specify the absolute lower-left corner of the text (if angle is 0) instead of the baseline of the first letter, making texts appear incorrectly. This however require fonts to have a steady baseline, like fx. Arial, Verdana, etc. unlike Comic + +Version 0.3.0 [2006-Mar-02] - Jesper Veggerby <pear.nosey@veggerby.dk> * Added clipping (GD uses Image_Canvas method involving ImageCopy(), until native support is available throught existing GD functions gdImageSetClip()) * Fixed the way SVG rotates text to now use transform * Added public reset() method to Image_Canvas http://cvs.php.net/viewcvs.cgi/pear/Image_Canvas/docs/README?r1=1.4&r2=1.5&diff_format=u Index: pear/Image_Canvas/docs/README diff -u pear/Image_Canvas/docs/README:1.4 pear/Image_Canvas/docs/README:1.5 --- pear/Image_Canvas/docs/README:1.4 Tue Nov 8 19:01:03 2005 +++ pear/Image_Canvas/docs/README Wed Mar 22 21:56:04 2006 @@ -1,7 +1,7 @@ ******************************* Image_Canvas ******************************* - version 0.2.4 + version 0.3.1 ------ CopyRight (C) Jesper Veggerby Hansen, 2003-2005 ------ http://pear.php.net/pepr/pepr-proposal-show.php?id=212 @@ -31,6 +31,7 @@ History ------- Release history of Image_Canvas +0.3.0 02-Mar-2006 0.2.4 08-Nov-2005 0.2.3 28-Oct-2005 0.2.2 30-Sep-2005 http://cvs.php.net/viewcvs.cgi/pear/Image_Canvas/Canvas/GD.php?r1=1.11&r2=1.12&diff_format=u Index: pear/Image_Canvas/Canvas/GD.php diff -u pear/Image_Canvas/Canvas/GD.php:1.11 pear/Image_Canvas/Canvas/GD.php:1.12 --- pear/Image_Canvas/Canvas/GD.php:1.11 Mon Feb 13 20:53:20 2006 +++ pear/Image_Canvas/Canvas/GD.php Wed Mar 22 21:56:04 2006 @@ -29,7 +29,7 @@ * @author Jesper Veggerby <pear.nosey@veggerby.dk> * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 - * @version CVS: $Id: GD.php,v 1.11 2006/02/13 20:53:20 nosey Exp $ + * @version CVS: $Id: GD.php,v 1.12 2006/03/22 21:56:04 nosey Exp $ * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=212 */ @@ -1288,13 +1288,13 @@ } $linebreaks = substr_count($text, "\n"); - if (($angle == 0) && ($linebreaks == 0) && ($force === false)) { + if (($angle == 0) && ($force === false)) { /* * if the angle is 0 simply return the size, due to different * heights for example for x-axis labels, making the labels * _not_ appear as written on the same baseline */ - return $this->_font['size'] + 2; + return $this->_font['size'] + ($this->_font['size'] + 2) * $linebreaks; } $height = 0; @@ -1325,6 +1325,105 @@ } } } + + /** + * Calculated the absolute bottom-left position of the text, by simulating + * the calculation of the baseline drop. + * @param int $x The relative x position to write the text + * @param int $y The relative y position to write the text + * @param string $text The text to write + * @param array $align The alignment of the text relative to (x, y) + * @returns array An array containing the absolute x and y positions + * @access private + */ + function _getAbsolutePosition($x, $y, $text, $align) + { + if ($this->_font['angle'] > 0) { + $w0 = $this->textWidth($text); + $h0 = $this->textHeight($text); + + if ($align['vertical'] == 'bottom') { + $dy = $y - $h0; + } + else if ($align['vertical'] == 'center') { + $dy = $y - $h0 / 2; + } + else if ($align['vertical'] == 'top') { + $dy = $y; + } + + if ($align['horizontal'] == 'right') { + $dx = $x - $w0; + } + else if ($align['horizontal'] == 'center') { + $dx = $x - $w0 / 2; + } + else if ($align['horizontal'] == 'left') { + $dx = $x; + } + + if (($this->_font['angle'] < 180) && ($this->_font['angle'] >= 0)) { + $dy += $h0; + } + if (($this->_font['angle'] >= 90) && ($this->_font['angle'] < 270)) { + $dx += $w0; + } + } + else { + // get the maximum size of normal text above base line - sampled by 'Al' + $size1 = imagettfbbox($this->_font['size'], 0, $this->_font['file'], 'Al'); + $height1 = abs($size1[7] - $size1[1]); + + // get the maximum size of all text above base and below line - sampled by 'AlgjpqyQ' + $size2 = imagettfbbox($this->_font['size'], 0, $this->_font['file'], 'AlgjpqyQ'); + $height2 = abs($size2[7] - $size2[1]); + + // get the size of the text, simulating height above baseline beinh max, by sampling using 'Al' + $size = imagettfbbox($this->_font['size'], 0, $this->_font['file'], 'Al' . $text); + $height = abs($size[7] - $size[1]); + + // if all text is above baseline, i.e. height of text compares to max height above (within 10%) + if (abs($height - $height1)/$height1 < 0.1) { + $dHeight = 0; + } + else { + $dHeight = abs($height2 - $height1); + } + + // specifies the bottom-left corner! + $dx = $x + sin(deg2rad($this->_font['angle'])) * $dHeight; + $dy = $y - cos(deg2rad($this->_font['angle'])) * $dHeight; + + if ($align['vertical'] == 'top') { + $dy += $height; + } + else if ($align['vertical'] == 'center') { + $dy += ($height + $dHeight) / 2; + } + else if ($align['vertical'] == 'bottom') { + $dy += $dHeight; + } + + if ($align['horizontal'] == 'center') { + $factor = 0.5; + } + else if ($align['horizontal'] == 'right') { + $factor = 1; + } + else { + $factor = 0; + } + + if ($factor != 0) { + $size = imagettfbbox($this->_font['size'], 0, $this->_font['file'], $text); + $w0 = abs($size[2] - $size[0]); + $dx -= cos(deg2rad($this->_font['angle'])) * $w0 * $factor; + $dy += sin(deg2rad($this->_font['angle'])) * $w0 * $factor; + } + } + + return array('x' => $dx, 'y' => $dy); + } /** * Writes text @@ -1362,55 +1461,54 @@ if (!isset($alignment['horizontal'])) { $alignment['horizontal'] = 'left'; } - - if ($alignment['vertical'] == 'bottom') { - $y0 = $y0 - $this->textHeight($text, true); - } elseif ($alignment['vertical'] == 'center') { - $y0 = $y0 - ($this->textHeight($text, true) / 2); - } + if (isset($this->_font['size'])) { + $textHeight = $this->_font['size'] + 2; + } + else { + $textHeight = $this->textHeight('A'); + } $lines = explode("\n", $text); - foreach ($lines as $line) { - $textWidth = $this->textWidth($line); - $textHeight = $this->textHeight($line, true); - - $x = $x0; + foreach ($lines as $line) { + $x = $x0; $y = $y0; $y0 += $textHeight + 2; - if ($alignment['horizontal'] == 'right') { - $x = $x - $textWidth; - } elseif ($alignment['horizontal'] == 'center') { - $x = $x - ($textWidth / 2); - } - if (($color === false) && (isset($this->_font['color']))) { $color = $this->_font['color']; } if ($color != 'transparent') { - if (isset($this->_font['file'])) { - if (($this->_font['angle'] < 180) && ($this->_font['angle'] >= 0)) { - $y += $textHeight; - } - if (($this->_font['angle'] >= 90) && ($this->_font['angle'] < 270)) { - $x += $textWidth; - } - + if (isset($this->_font['file'])) { + $result = $this->_getAbsolutePosition($x, $y, $line, $alignment); ImageTTFText( $this->_canvas, $this->_font['size'], $this->_font['angle'], - $x, - $y, + $result['x'], + $result['y'], $this->_color($color), $this->_font['file'], $line ); } else { - if ((isset($this->_font['vertical'])) && ($this->_font['vertical'])) { + $width = $this->textWidth($line); + $height = $this->textHeight($line); + if ($alignment['horizontal'] == 'right') { + $x -= $width; + } + else if ($alignment['horizontal'] == 'center') { + $x -= $width / 2; + } + if ($alignment['vertical'] == 'bottom') { + $y -= $height; + } + else if ($alignment['vertical'] == 'center') { + $y -= $height / 2; + } + if ((isset($this->_font['vertical'])) && ($this->_font['vertical'])) { ImageStringUp( $this->_canvas, $this->_font['font'],
« previous php.pear.cvs (#39253) next »