[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/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   
  11  /**
  12   * Built-in PHP fonts
  13   *
  14   * @package Artichow
  15   */
  16  class awFont {
  17      
  18      /**
  19       * Used font
  20       * 
  21       * @param int $font
  22       */
  23      public $font;
  24      
  25      /**
  26       * Build the font
  27       *
  28       * @param int $font Font identifier
  29       */
  30  	public function __construct($font) {
  31      
  32          $this->font = $font;
  33      
  34      }
  35      
  36      /**
  37       * Draw a text
  38       *
  39       * @param awDrawer $drawer
  40       * @param awPoint $p Draw text at this point
  41       * @param awText $text The text
  42       */
  43  	public function draw(awDrawer $drawer, awPoint $p, awText $text) {
  44      
  45          $angle = $text->getAngle();
  46      
  47          if($angle !== 90 and $angle !== 0) {
  48              trigger_error("You can only use 0° and 90°", E_USER_ERROR);
  49          }
  50          
  51          if($angle === 90) {
  52              $function = 'imagestringup';
  53          } else {
  54              $function = 'imagestring';
  55          }
  56          
  57          if($angle === 90) {
  58              $add = $this->getTextHeight($text);
  59          } else {
  60              $add = 0;
  61          }
  62      
  63          $color = $text->getColor();
  64          $rgb = $color->getColor($drawer->resource);
  65          
  66          $function(
  67              $drawer->resource,
  68              $this->font,
  69              $drawer->x + $p->x,
  70              $drawer->y + $p->y + $add,
  71              $text->getText(),
  72              $rgb
  73          );
  74      
  75      }
  76      
  77      /**
  78       * Get the width of a string
  79       *
  80       * @param awText $text A string
  81       */
  82  	public function getTextWidth(awText $text) {
  83      
  84          if($text->getAngle() === 90) {
  85              $text->setAngle(45);
  86              return $this->getTextHeight($text);
  87          } else if($text->getAngle() === 45) {
  88              $text->setAngle(90);
  89          }
  90          
  91          $font = $text->getFont();
  92          $fontWidth = imagefontwidth($font->font);
  93          
  94          if($fontWidth === FALSE) {
  95              trigger_error("Unable to get font size", E_USER_ERROR);
  96          }
  97          
  98          return (int)$fontWidth * strlen($text->getText());
  99      
 100      }
 101      
 102      /**
 103       * Get the height of a string
 104       *
 105       * @param awText $text A string
 106       */
 107  	public function getTextHeight(awText $text) {
 108      
 109          if($text->getAngle() === 90) {
 110              $text->setAngle(45);
 111              return $this->getTextWidth($text);
 112          } else if($text->getAngle() === 45) {
 113              $text->setAngle(90);
 114          }
 115          
 116          $font = $text->getFont();
 117          $fontHeight = imagefontheight($font->font);
 118          
 119          if($fontHeight === FALSE) {
 120              trigger_error("Unable to get font size", E_USER_ERROR);
 121          }
 122          
 123          return (int)$fontHeight;
 124  
 125      }
 126  
 127  }
 128  
 129  registerClass('Font');
 130  
 131  /**
 132   * TTF fonts
 133   *
 134   * @package Artichow
 135   */
 136  class awTTFFont extends awFont {
 137  
 138      /**
 139       * Font size
 140       *
 141       * @var int
 142       */
 143      public $size;
 144  
 145      /**
 146       * Font file
 147       *
 148       * @param string $font Font file
 149       * @param int $size Font size
 150       */
 151  	public function __construct($font, $size) {
 152      
 153          parent::__construct($font);
 154          
 155          $this->size = (int)$size;
 156      
 157      }
 158      
 159      /**
 160       * Draw a text
 161       *
 162       * @param awDrawer $drawer
 163       * @param awPoint $p Draw text at this point
 164       * @param awText $text The text
 165       */
 166  	public function draw(awDrawer $drawer, awPoint $p, awText $text) {
 167      
 168          // Make easier font positionment
 169          $text->setText($text->getText()." ");
 170      
 171          $color = $text->getColor();
 172          $rgb = $color->getColor($drawer->resource);
 173          
 174          $box = imagettfbbox($this->size, $text->getAngle(), $this->font, $text->getText());
 175          
 176          $height =  - $box[5];
 177          
 178          $box = imagettfbbox($this->size, 90, $this->font, $text->getText());
 179          $width = abs($box[6] - $box[2]);
 180      
 181          // Restore old text
 182          $text->setText(substr($text->getText(), 0, strlen($text->getText()) - 1));
 183          
 184          imagettftext(
 185              $drawer->resource,
 186              $this->size,
 187              $text->getAngle(),
 188              $drawer->x + $p->x + $width  * sin($text->getAngle() / 180 * M_PI),
 189              $drawer->y + $p->y + $height,
 190              $rgb,
 191              $this->font,
 192              $text->getText()
 193          );
 194          
 195      }
 196      
 197      /**
 198       * Get the width of a string
 199       *
 200       * @param awText $text A string
 201       */
 202  	public function getTextWidth(awText $text) {
 203          
 204          $box = imagettfbbox($this->size, $text->getAngle(), $this->font, $text->getText());
 205          
 206          if($box === FALSE) {
 207              trigger_error("Unable to get font size", E_USER_ERROR);
 208              return;
 209          }
 210          
 211          list(, , $x2, $y2, , , $x1, $y1) = $box;
 212          
 213          return abs($x2 - $x1);
 214      
 215      }
 216      
 217      /**
 218       * Get the height of a string
 219       *
 220       * @param awText $text A string
 221       */
 222  	public function getTextHeight(awText $text) {
 223          
 224          $box = imagettfbbox($this->size, $text->getAngle(), $this->font, $text->getText());
 225          
 226          if($box === FALSE) {
 227              trigger_error("Unable to get font size", E_USER_ERROR);
 228              return;
 229          }
 230          
 231          list(, , $x2, $y2, , , $x1, $y1) = $box;
 232          
 233          return abs($y2 - $y1);
 234  
 235      }
 236  
 237  }
 238  
 239  registerClass('TTFFont');
 240  
 241  /* <php5> */
 242  
 243  $php = '';
 244  
 245  for($i = 1; $i <= 5; $i++) {
 246  
 247      $php .= '
 248      class awFont'.$i.' extends awFont {
 249      
 250  		public function __construct() {
 251              parent::__construct('.$i.');
 252          }
 253      
 254      }
 255      ';
 256      
 257      if(ARTICHOW_PREFIX !== 'aw') {
 258          $php .= '
 259          class '.ARTICHOW_PREFIX.'Font'.$i.' extends awFont'.$i.' {
 260          }
 261          ';
 262      }
 263  
 264  }
 265  
 266  eval($php);
 267  
 268  $php = '';
 269  
 270  foreach($fonts as $font) {
 271  
 272      $php .= '
 273      class aw'.$font.' extends awTTFFont {
 274      
 275  		public function __construct($size) {
 276              parent::__construct(\''.(ARTICHOW_FONT.DIRECTORY_SEPARATOR.$font.'.ttf').'\', $size);
 277          }
 278      
 279      }
 280      ';
 281      
 282      if(ARTICHOW_PREFIX !== 'aw') {
 283          $php .= '
 284          class '.ARTICHOW_PREFIX.$font.' extends aw'.$font.' {
 285          }
 286          ';
 287      }
 288  
 289  }
 290  
 291  eval($php);
 292  
 293  /* </php5> */
 294  /* <php4> --
 295  
 296  $php = '';
 297  
 298  for($i = 1; $i <= 5; $i++) {
 299  
 300      $php .= '
 301      class awFont'.$i.' extends awFont {
 302      
 303          function awFont'.$i.'() {
 304              parent::awFont('.$i.');
 305          }
 306      
 307      }
 308      ';
 309      
 310      if(ARTICHOW_PREFIX !== 'aw') {
 311          $php .= '
 312          class '.ARTICHOW_PREFIX.'Font'.$i.' extends awFont'.$i.' {
 313          }
 314          ';
 315      }
 316  
 317  }
 318  
 319  eval($php);
 320  
 321  $php = '';
 322  
 323  foreach($fonts as $font) {
 324  
 325      $php .= '
 326      class aw'.$font.' extends awTTFFont {
 327      
 328          function aw'.$font.'($size) {
 329              parent::awTTFFont(\''.(ARTICHOW_FONT.DIRECTORY_SEPARATOR.$font.'.ttf').'\', $size);
 330          }
 331      
 332      }
 333      ';
 334      
 335      if(ARTICHOW_PREFIX !== 'aw') {
 336          $php .= '
 337          class '.ARTICHOW_PREFIX.$font.' extends aw'.$font.' {
 338          }
 339          ';
 340      }
 341  
 342  }
 343  
 344  eval($php);
 345  
 346  -- </php4> */
 347  
 348  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics