[ 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/php4/inc/ -> Label.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  /* <php4> */
  12  
  13  define("LABEL_LEFT", 1);
  14  define("LABEL_RIGHT", 2);
  15  define("LABEL_CENTER", 3);
  16  define("LABEL_TOP", 4);
  17  define("LABEL_BOTTOM", 5);
  18  define("LABEL_MIDDLE", 6);
  19  
  20  /* </php4> */
  21   
  22  /**
  23   * Draw labels
  24   *
  25   * @package Artichow
  26   */
  27  class awLabel {
  28  
  29      /**
  30       * Label border
  31       *
  32       * @var int
  33       */
  34      var $border;
  35  
  36      /**
  37       * Label texts
  38       *
  39       * @var array
  40       */
  41      var $texts;
  42  
  43      /**
  44       * Text font
  45       *
  46       * @var int
  47       */
  48      var $font;
  49  
  50      /**
  51       * Text angle
  52       *
  53       * @var int
  54       */
  55      var $angle = 0;
  56  
  57      /**
  58       * Text color
  59       *
  60       * @var Color
  61       */
  62      var $color;
  63  
  64      /**
  65       * Text background
  66       *
  67       * @var Color, Gradient
  68       */
  69      var $background;
  70  
  71      /**
  72       * Callback function
  73       *
  74       * @var string
  75       */
  76      var $function;
  77  
  78      /**
  79       * Padding
  80       *
  81       * @var int
  82       */
  83      var $padding;
  84  
  85      /**
  86       * Move position from this vector
  87       *
  88       * @var Point
  89       */
  90      var $move;
  91  
  92      /**
  93       * Label interval
  94       *
  95       * @var int
  96       */
  97      var $interval = 1;
  98  
  99      /**
 100       * Horizontal align
 101       *
 102       * @var int
 103       */
 104      var $hAlign = LABEL_CENTER;
 105  
 106      /**
 107       * Vertical align
 108       *
 109       * @var int
 110       */
 111      var $vAlign = LABEL_MIDDLE;
 112      
 113      /**
 114       * Hide all labels ?
 115       *
 116       * @var bool
 117       */
 118      var $hide = FALSE;
 119      
 120      /**
 121       * Keys to hide
 122       *
 123       * @var array
 124       */
 125      var $hideKey = array();
 126      
 127      /**
 128       * Values to hide
 129       *
 130       * @var array
 131       */
 132      var $hideValue = array();
 133      
 134      /**
 135       * Hide first label
 136       *
 137       * @var bool
 138       */
 139      var $hideFirst = FALSE;
 140      
 141      /**
 142       * Hide last label
 143       *
 144       * @var bool
 145       */
 146      var $hideLast = FALSE;
 147      
 148      /**
 149       * Build the label
 150       *
 151       * @param string $label First label
 152       */
 153  	 function awLabel($label = NULL, $font = NULL, $color = NULL, $angle = 0) {
 154      
 155          if(is_array($label)) {
 156              $this->set($label);
 157          } else if(is_string($label)) {
 158              $this->set(array($label));
 159          }
 160          
 161          if($font === NULL) {
 162              $font = new awFont2;
 163          }
 164          
 165          $this->setFont($font);
 166          $this->setAngle($angle);
 167          
 168          if(is_a($color, 'awColor')) {
 169              $this->setColor($color);
 170          } else {
 171              $this->setColor(new awColor(0, 0, 0));
 172          }
 173          
 174          $this->move = new awPoint(0, 0);
 175          
 176          $this->border = new awBorder;
 177          $this->border->hide();
 178          
 179      }
 180      
 181      /**
 182       * Get an element of the label from its key
 183       *
 184       * @param int $key Element key
 185       * @return string A value
 186       */
 187  	 function get($key) {
 188          return array_key_exists($key, $this->texts) ? $this->texts[$key] : NULL;
 189      }
 190      
 191      /**
 192       * Get all labels
 193       *
 194       * @return array
 195       */
 196  	 function all() {
 197          return $this->texts;
 198      }
 199      
 200      /**
 201       * Set one or several labels
 202       *
 203       * @param array $labels Array of string or a string
 204       */
 205  	 function set($labels) {
 206      
 207          if(is_string($labels)) {
 208              $this->texts = array($labels);
 209          } else if(is_array($labels)) {
 210              $this->texts = $labels;
 211          }
 212          
 213      }
 214      
 215      /**
 216       * Count number of texts in the label
 217       *
 218       * @return int
 219       */
 220  	 function count() {
 221          return is_array($this->texts) ? count($this->texts) : 0;
 222      }
 223      
 224      /**
 225       * Set a callback function for labels
 226       *
 227       * @param string $function
 228       */
 229  	 function setCallbackFunction($function) {
 230          $this->function = is_null($function) ? $function : (string)$function;
 231      }
 232      
 233      /**
 234       * Return the callback function for labels
 235       *
 236       * @return string
 237       */
 238  	 function getCallbackFunction() {
 239          return $this->function;
 240      }
 241      
 242      /**
 243       * Change labels format
 244       *
 245       * @param string $format New format (printf style: %.2f for example)
 246       */
 247  	 function setFormat($format) {
 248          $function = 'label'.time().'_'.(microtime() * 1000000);
 249          eval('function '.$function.'($value) {
 250              return sprintf("'.addcslashes($format, '"').'", $value);
 251          }');
 252          $this->setCallbackFunction($function);
 253      }
 254      
 255      /**
 256       * Change font for label
 257       *
 258       * @param &$font New font
 259       * @param $color Font color (can be NULL)
 260       */
 261  	 function setFont(&$font, $color = NULL) {
 262          $this->font = $font;
 263          if(is_a($color, 'awColor')) {
 264              $this->setColor($color);
 265          }
 266      }
 267      
 268      /**
 269       * Change font angle
 270       *
 271       * @param int $angle New angle
 272       */
 273  	 function setAngle($angle) {
 274          $this->angle = (int)$angle;
 275      }
 276      
 277      /**
 278       * Change font color
 279       *
 280       * @param $color
 281       */
 282  	 function setColor($color) {
 283          $this->color = $color;
 284      }
 285      
 286      /**
 287       * Change text background
 288       *
 289       * @param mixed $background
 290       */
 291  	 function setBackground($background) {
 292          $this->background = $background;
 293      }
 294      
 295      /**
 296       * Change text background color
 297       *
 298       * @param Color
 299       */
 300  	 function setBackgroundColor($color) {
 301          $this->background = $color;
 302      }
 303      
 304      /**
 305       * Change text background gradient
 306       *
 307       * @param Gradient
 308       */
 309  	 function setBackgroundGradient($gradient) {
 310          $this->background = $gradient;
 311      }
 312  
 313      /**
 314       * Change padding
 315       *
 316       * @param int $left Left padding
 317       * @param int $right Right padding
 318       * @param int $top Top padding
 319       * @param int $bottom Bottom padding
 320       */
 321  	 function setPadding($left, $right, $top, $bottom) {
 322          $this->padding = array((int)$left, (int)$right, (int)$top, (int)$bottom);
 323      }
 324      
 325      /**
 326       * Hide all labels ?
 327       *
 328       * @param bool $hide
 329       */
 330  	 function hide($hide = TRUE) {
 331          $this->hide = (bool)$hide;
 332      }
 333      
 334      /**
 335       * Show all labels ?
 336       *
 337       * @param bool $show
 338       */
 339  	 function show($show = TRUE) {
 340          $this->hide = (bool)!$show;
 341      }
 342      
 343      /**
 344       * Hide a key
 345       *
 346       * @param int $key The key to hide
 347       */
 348  	 function hideKey($key) {
 349          $this->hideKey[$key] = TRUE;
 350      }
 351      
 352      /**
 353       * Hide a value
 354       *
 355       * @param int $value The value to hide
 356       */
 357  	 function hideValue($value) {
 358          $this->hideValue[] = $value;
 359      }
 360      
 361      /**
 362       * Hide first label
 363       *
 364       * @param bool $hide
 365       */
 366  	 function hideFirst($hide) {
 367          $this->hideFirst = (bool)$hide;
 368      }
 369      
 370      /**
 371       * Hide last label
 372       *
 373       * @param bool $hide
 374       */
 375  	 function hideLast($hide) {
 376          $this->hideLast = (bool)$hide;
 377      }
 378      
 379      /**
 380       * Set label interval
 381       *
 382       * @param int
 383       */
 384  	 function setInterval($interval) {
 385      
 386          $this->interval = (int)$interval;
 387          
 388      }
 389      
 390      /**
 391       * Change label position
 392       *
 393       * @param int $x Add this interval to X coord
 394       * @param int $y Add this interval to Y coord
 395       */
 396  	 function move($x, $y) {
 397      
 398          $this->move = $this->move->move($x, $y);
 399      
 400      }
 401      
 402      /**
 403       * Change alignment
 404       *
 405       * @param int $h Horizontal alignment
 406       * @param int $v Vertical alignment
 407       */
 408  	 function setAlign($h = NULL, $v = NULL) {
 409          if($h !== NULL) {
 410              $this->hAlign = (int)$h;
 411          }
 412          if($v !== NULL) {
 413              $this->vAlign = (int)$v;
 414          }
 415      }
 416      
 417      /**
 418       * Get a text from the labele
 419       *
 420       * @param mixed $key Key in the array text
 421       * @return Text
 422       */
 423  	 function getText($key) {
 424      
 425          if(is_array($this->texts) and array_key_exists($key, $this->texts)) {
 426          
 427              $value = $this->texts[$key];
 428              
 429              if(is_string($this->function)) {
 430                  $value = call_user_func($this->function, $value);
 431              }
 432          
 433              $text = new awText($value);
 434              $text->setFont($this->font);
 435              $text->setAngle($this->angle);
 436              $text->setColor($this->color);
 437              
 438              if(is_a($this->background, 'awColor')) {
 439                  $text->setBackgroundColor($this->background);
 440              } else if(is_a($this->background, 'awGradient')) {
 441                  $text->setBackgroundGradient($this->background);
 442              }
 443              
 444              $text->border = $this->border;
 445              
 446              if($this->padding !== NULL) {
 447                  call_user_func_array(array($text, 'setPadding'), $this->padding);
 448              }
 449              
 450              return $text;
 451              
 452          } else {
 453              return NULL;
 454          }
 455      
 456      }
 457      
 458      /**
 459       * Get max width of all texts
 460       *
 461       * @param $drawer A drawer
 462       * @return int
 463       */
 464  	 function getMaxWidth($drawer) {
 465      
 466          return $this->getMax($drawer, 'getTextWidth');
 467      
 468      }
 469      
 470      /**
 471       * Get max height of all texts
 472       *
 473       * @param $drawer A drawer
 474       * @return int
 475       */
 476  	 function getMaxHeight($drawer) {
 477      
 478          return $this->getMax($drawer, 'getTextHeight');
 479          
 480      }
 481      
 482      /**
 483       * Draw the label
 484       *
 485       * @param $drawer
 486       * @param $p Label center
 487       * @param int $key Text position in the array of texts (default to zero)
 488       */
 489  	 function draw($drawer, $p, $key = 0) {
 490  
 491          if(($key % $this->interval) !== 0) {
 492              return;
 493          }
 494      
 495          // Hide all labels
 496          if($this->hide) {
 497              return;
 498          }
 499          
 500          // Key is hidden
 501          if(array_key_exists($key, $this->hideKey)) {
 502              return;
 503          }
 504          
 505          // Hide first label
 506          if($key === 0 and $this->hideFirst) {
 507              return;
 508          }
 509          
 510          // Hide last label
 511          if($key === count($this->texts) - 1 and $this->hideLast) {
 512              return;
 513          }
 514      
 515          $text = $this->getText($key);
 516          
 517          if($text !== NULL) {
 518          
 519              // Value must be hidden
 520              if(in_array($text->getText(), $this->hideValue)) {
 521                  return;
 522              }
 523          
 524              $x = $p->x;
 525              $y = $p->y;
 526              
 527              // Get padding
 528              list($left, $right, $top, $bottom) = $text->getPadding();
 529              
 530              $font = $text->getFont();
 531              $width = $font->getTextWidth($text);
 532              $height = $font->getTextHeight($text);
 533              
 534              switch($this->hAlign) {
 535              
 536                  case LABEL_RIGHT :
 537                      $x -= ($width + $right);
 538                      break;
 539              
 540                  case LABEL_CENTER :
 541                      $x -= ($width - $left + $right) / 2;
 542                      break;
 543              
 544                  case LABEL_LEFT :
 545                      $x += $left;
 546                      break;
 547              
 548              }
 549              
 550              switch($this->vAlign) {
 551              
 552                  case LABEL_TOP :
 553                      $y -= ($height + $bottom);
 554                      break;
 555              
 556                  case LABEL_MIDDLE :
 557                      $y -= ($height - $top + $bottom) / 2;
 558                      break;
 559              
 560                  case LABEL_BOTTOM :
 561                      $y += $top;
 562                      break;
 563              
 564              }
 565          
 566              $drawer->string($text, $this->move->move($x, $y));
 567              
 568          }
 569          
 570      }
 571      
 572  	 function getMax($drawer, $function) {
 573      
 574          $max = NULL;
 575      
 576          foreach($this->texts as $key => $text) {
 577          
 578              $text = $this->getText($key);
 579              $font = $text->getFont();
 580          
 581              if(is_null($max)) {
 582                  $max = $font->{$function}($text);
 583              } else {
 584                  $max = max($max, $font->{$function}($text));
 585              }
 586          
 587          }
 588          
 589          return $max;
 590          
 591      }
 592  
 593  }
 594  
 595  registerClass('Label');
 596  ?>


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