[ Index ] |
|
Code source de Cr@wltr@ck 2.2.1 |
1 <?php 2 /* 3 * This work is hereby released into the Public Domain. 4 * To view a copy of the public domain dedication, 5 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to 6 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 7 * 8 */ 9 10 require_once dirname(__FILE__)."/../Graph.class.php"; 11 12 13 /** 14 * Built-in PHP fonts 15 * 16 * @package Artichow 17 */ 18 class awFont { 19 20 /** 21 * Used font 22 * 23 * @param int $font 24 */ 25 var $font; 26 27 /** 28 * Build the font 29 * 30 * @param int $font Font identifier 31 */ 32 function awFont($font) { 33 34 $this->font = $font; 35 36 } 37 38 /** 39 * Draw a text 40 * 41 * @param $drawer 42 * @param $p Draw text at this point 43 * @param &$text The text 44 * @param &$width Text box width 45 */ 46 function draw($drawer, $p, &$text, $width = NULL) { 47 48 $angle = $text->getAngle(); 49 50 if($angle !== 90 and $angle !== 0) { 51 awImage::drawError("Class Font: You can only use 0° and 90° angles."); 52 } 53 54 if($angle === 90) { 55 $function = 'imagestringup'; 56 } else { 57 $function = 'imagestring'; 58 } 59 60 if($angle === 90) { 61 $addAngle = $this->getTextHeight($text); 62 } else { 63 $addAngle = 0; 64 } 65 66 $color = $text->getColor(); 67 $rgb = $color->getColor($drawer->resource); 68 69 $textString = $text->getText(); 70 $textString = str_replace("\r", "", $textString); 71 $textHeight = $this->getTextHeight($text); 72 73 // Split text if needed 74 if($width !== NULL) { 75 76 $characters = floor($width / ($this->getTextWidth($text) / strlen($textString))); 77 78 $textString = wordwrap($textString, $characters, "\n", TRUE); 79 80 } 81 82 $lines = explode("\n", $textString); 83 84 foreach($lines as $i => $line) { 85 86 // Line position handling 87 if($angle === 90) { 88 $addX = $i * $textHeight; 89 $addY = 0; 90 } else { 91 $addX = 0; 92 $addY = $i * $textHeight; 93 } 94 95 $function( 96 $drawer->resource, 97 $this->font, 98 $drawer->x + $p->x + $addX, 99 $drawer->y + $p->y + $addY + $addAngle, 100 $line, 101 $rgb 102 ); 103 104 } 105 106 } 107 108 /** 109 * Get the width of a string 110 * 111 * @param &$text A string 112 */ 113 function getTextWidth(&$text) { 114 115 if($text->getAngle() === 90) { 116 $text->setAngle(45); 117 return $this->getTextHeight($text); 118 } else if($text->getAngle() === 45) { 119 $text->setAngle(90); 120 } 121 122 $font = $text->getFont(); 123 $fontWidth = imagefontwidth($font->font); 124 125 if($fontWidth === FALSE) { 126 awImage::drawError("Class Font: Unable to get font size."); 127 } 128 129 return (int)$fontWidth * strlen($text->getText()); 130 131 } 132 133 /** 134 * Get the height of a string 135 * 136 * @param &$text A string 137 */ 138 function getTextHeight(&$text) { 139 140 if($text->getAngle() === 90) { 141 $text->setAngle(45); 142 return $this->getTextWidth($text); 143 } else if($text->getAngle() === 45) { 144 $text->setAngle(90); 145 } 146 147 $font = $text->getFont(); 148 $fontHeight = imagefontheight($font->font); 149 150 if($fontHeight === FALSE) { 151 awImage::drawError("Class Font: Unable to get font size."); 152 } 153 154 return (int)$fontHeight; 155 156 } 157 158 } 159 160 registerClass('Font'); 161 162 /** 163 * TTF fonts 164 * 165 * @package Artichow 166 */ 167 class awTTFFont extends awFont { 168 169 /** 170 * Font size 171 * 172 * @var int 173 */ 174 var $size; 175 176 /** 177 * Font file 178 * 179 * @param string $font Font file 180 * @param int $size Font size 181 */ 182 function awTTFFont($font, $size) { 183 184 parent::awFont($font); 185 186 $this->size = (int)$size; 187 188 } 189 190 /** 191 * Draw a text 192 * 193 * @param $drawer 194 * @param $p Draw text at this point 195 * @param &$text The text 196 * @param &$width Text box width 197 */ 198 function draw($drawer, $p, &$text, $width = NULL) { 199 200 // Make easier font positionment 201 $text->setText($text->getText()." "); 202 203 $color = $text->getColor(); 204 $rgb = $color->getColor($drawer->resource); 205 206 $box = imagettfbbox($this->size, $text->getAngle(), $this->font, $text->getText()); 207 208 $textHeight = - $box[5]; 209 210 $box = imagettfbbox($this->size, 90, $this->font, $text->getText()); 211 $textWidth = abs($box[6] - $box[2]); 212 213 // Restore old text 214 $text->setText(substr($text->getText(), 0, strlen($text->getText()) - 1)); 215 216 $textString = $text->getText(); 217 218 // Split text if needed 219 if($width !== NULL) { 220 221 $characters = floor($width / $this->getAverageWidth()); 222 $textString = wordwrap($textString, $characters, "\n", TRUE); 223 224 } 225 226 imagettftext( 227 $drawer->resource, 228 $this->size, 229 $text->getAngle(), 230 $drawer->x + $p->x + $textWidth * sin($text->getAngle() / 180 * M_PI), 231 $drawer->y + $p->y + $textHeight, 232 $rgb, 233 $this->font, 234 $textString 235 ); 236 237 } 238 239 /** 240 * Get the width of a string 241 * 242 * @param &$text A string 243 */ 244 function getTextWidth(&$text) { 245 246 $box = imagettfbbox($this->size, $text->getAngle(), $this->font, $text->getText()); 247 248 if($box === FALSE) { 249 awImage::drawError("Class TTFFont: Unable to get font size."); 250 } 251 252 list(, , $x2, $y2, , , $x1, $y1) = $box; 253 254 return abs($x2 - $x1); 255 256 } 257 258 /** 259 * Get the height of a string 260 * 261 * @param &$text A string 262 */ 263 function getTextHeight(&$text) { 264 265 $box = imagettfbbox($this->size, $text->getAngle(), $this->font, $text->getText()); 266 267 if($box === FALSE) { 268 awImage::drawError("Class TTFFont: Unable to get font size."); 269 } 270 271 list(, , $x2, $y2, , , $x1, $y1) = $box; 272 273 return abs($y2 - $y1); 274 275 } 276 277 /** 278 * Get average width of a character 279 * 280 * @return int 281 */ 282 function getAverageWidth() { 283 284 $text = "azertyuiopqsdfghjklmmmmmmmwxcvbbbn,;:!?."; 285 286 $box = imagettfbbox($this->size, 0, $this->font, $text); 287 288 if($box === FALSE) { 289 awImage::drawError("Class TTFFont: Unable to get font size."); 290 } 291 292 list(, , $x2, $y2, , , $x1, $y1) = $box; 293 294 return abs($x2 - $x1) / strlen($text); 295 296 } 297 298 } 299 300 registerClass('TTFFont'); 301 302 303 304 $php = ''; 305 306 for($i = 1; $i <= 5; $i++) { 307 308 $php .= ' 309 class awFont'.$i.' extends awFont { 310 311 function awFont'.$i.'() { 312 parent::awFont('.$i.'); 313 } 314 315 } 316 '; 317 318 if(ARTICHOW_PREFIX !== 'aw') { 319 $php .= ' 320 class '.ARTICHOW_PREFIX.'Font'.$i.' extends awFont'.$i.' { 321 } 322 '; 323 } 324 325 } 326 327 eval($php); 328 329 $php = ''; 330 331 foreach($fonts as $font) { 332 333 $php .= ' 334 class aw'.$font.' extends awTTFFont { 335 336 function aw'.$font.'($size) { 337 parent::awTTFFont(\''.(ARTICHOW_FONT.DIRECTORY_SEPARATOR.$font.'.ttf').'\', $size); 338 } 339 340 } 341 '; 342 343 if(ARTICHOW_PREFIX !== 'aw') { 344 $php .= ' 345 class '.ARTICHOW_PREFIX.$font.' extends aw'.$font.' { 346 } 347 '; 348 } 349 350 } 351 352 eval($php); 353 //mod to be able to use graph without ttf support using modification propose by Joel Alexandre http://paradigma.pt/ja/slog/ 354 if(!class_exists('awTuffy')) 355 { 356 class awTuffy extends awFont 357 { 358 359 function awTuffy($size) 360 { 361 $font = 1; 362 363 switch ($size) 364 { 365 case '7': 366 $font = 1; 367 break; 368 case '10': 369 $font = 2; 370 break; 371 case '16': 372 $font = 3; 373 break; 374 } 375 $this->font = $font; 376 } 377 } 378 } 379 380 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Sep 6 14:14:11 2007 | par Balluche grâce à PHPXref 0.7 |