[ Index ]
 

Code source de Cr@wltr@ck 2.2.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/graphs/artichow/php5/inc/ -> Font.class.php (source)

   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      public $font;
  26      
  27      /**
  28       * Build the font
  29       *
  30       * @param int $font Font identifier
  31       */
  32  	public function __construct($font) {
  33      
  34          $this->font = $font;
  35      
  36      }
  37      
  38      /**
  39       * Draw a text
  40       *
  41       * @param awDrawer $drawer
  42       * @param awPoint $p Draw text at this point
  43       * @param awText $text The text
  44       * @param awText $width Text box width
  45       */
  46  	public function draw(awDrawer $drawer, awPoint $p, awText $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 awText $text A string
 112       */
 113  	public function getTextWidth(awText $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 awText $text A string
 137       */
 138  	public function getTextHeight(awText $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      public $size;
 175  
 176      /**
 177       * Font file
 178       *
 179       * @param string $font Font file
 180       * @param int $size Font size
 181       */
 182  	public function __construct($font, $size) {
 183      
 184          parent::__construct($font);
 185          
 186          $this->size = (int)$size;
 187      
 188      }
 189      
 190      /**
 191       * Draw a text
 192       *
 193       * @param awDrawer $drawer
 194       * @param awPoint $p Draw text at this point
 195       * @param awText $text The text
 196       * @param awText $width Text box width
 197       */
 198  	public function draw(awDrawer $drawer, awPoint $p, awText $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 awText $text A string
 243       */
 244  	public function getTextWidth(awText $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 awText $text A string
 262       */
 263  	public function getTextHeight(awText $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  	protected 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  /* <php5> */
 303  
 304  $php = '';
 305  
 306  for($i = 1; $i <= 5; $i++) {
 307  
 308      $php .= '
 309      class awFont'.$i.' extends awFont {
 310      
 311  		public function __construct() {
 312              parent::__construct('.$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  		public function __construct($size) {
 337              parent::__construct(\''.(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  
 354  /* </php5> */
 355  /* <php4> --
 356  
 357  $php = '';
 358  
 359  for($i = 1; $i <= 5; $i++) {
 360  
 361      $php .= '
 362      class awFont'.$i.' extends awFont {
 363      
 364          function awFont'.$i.'() {
 365              parent::awFont('.$i.');
 366          }
 367      
 368      }
 369      ';
 370      
 371      if(ARTICHOW_PREFIX !== 'aw') {
 372          $php .= '
 373          class '.ARTICHOW_PREFIX.'Font'.$i.' extends awFont'.$i.' {
 374          }
 375          ';
 376      }
 377  
 378  }
 379  
 380  eval($php);
 381  
 382  $php = '';
 383  
 384  foreach($fonts as $font) {
 385  
 386      $php .= '
 387      class aw'.$font.' extends awTTFFont {
 388      
 389          function aw'.$font.'($size) {
 390              parent::awTTFFont(\''.(ARTICHOW_FONT.DIRECTORY_SEPARATOR.$font.'.ttf').'\', $size);
 391          }
 392      
 393      }
 394      ';
 395      
 396      if(ARTICHOW_PREFIX !== 'aw') {
 397          $php .= '
 398          class '.ARTICHOW_PREFIX.$font.' extends aw'.$font.' {
 399          }
 400          ';
 401      }
 402  
 403  }
 404  
 405  eval($php);
 406  
 407  -- </php4> */
 408  //mod to be able to use graph without ttf support using  modification propose by Joel Alexandre  http://paradigma.pt/ja/slog/
 409  if(!class_exists('awTuffy'))
 410  {
 411      class awTuffy extends awFont
 412      {
 413  
 414  		function awTuffy($size)
 415          {
 416              $font = 1;
 417  
 418              switch ($size)
 419              {
 420                  case '7':
 421                  $font = 1;
 422                  break;
 423                  case '10':
 424                  $font = 2;
 425                  break;
 426                  case '16':
 427                  $font = 3;
 428                  break;
 429              }
 430              $this->font = $font;
 431          }
 432      }
 433  }
 434  ?>


Généré le : Thu Sep 6 14:14:11 2007 par Balluche grâce à PHPXref 0.7