[ 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/ -> 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 implements awPositionable {
  28  
  29      /**
  30       * Label border
  31       *
  32       * @var int
  33       */
  34      public $border;
  35  
  36      /**
  37       * Label texts
  38       *
  39       * @var array
  40       */
  41      protected $texts;
  42  
  43      /**
  44       * Text font
  45       *
  46       * @var int
  47       */
  48      protected $font;
  49  
  50      /**
  51       * Text angle
  52       *
  53       * @var int
  54       */
  55      protected $angle = 0;
  56  
  57      /**
  58       * Text color
  59       *
  60       * @var Color
  61       */
  62      protected $color;
  63  
  64      /**
  65       * Text background
  66       *
  67       * @var Color, Gradient
  68       */
  69      private $background;
  70  
  71      /**
  72       * Callback function
  73       *
  74       * @var string
  75       */
  76      private $function;
  77  
  78      /**
  79       * Padding
  80       *
  81       * @var int
  82       */
  83      private $padding;
  84  
  85      /**
  86       * Move position from this vector
  87       *
  88       * @var Point
  89       */
  90      protected $move;
  91  
  92      /**
  93       * Label interval
  94       *
  95       * @var int
  96       */
  97      protected $interval = 1;
  98  
  99      /**
 100       * Horizontal align
 101       *
 102       * @var int
 103       */
 104      protected $hAlign = awLabel::CENTER;
 105  
 106      /**
 107       * Vertical align
 108       *
 109       * @var int
 110       */
 111      protected $vAlign = awLabel::MIDDLE;
 112      
 113      /**
 114       * Hide all labels ?
 115       *
 116       * @var bool
 117       */
 118      protected $hide = FALSE;
 119      
 120      /**
 121       * Keys to hide
 122       *
 123       * @var array
 124       */
 125      protected $hideKey = array();
 126      
 127      /**
 128       * Values to hide
 129       *
 130       * @var array
 131       */
 132      protected $hideValue = array();
 133      
 134      /**
 135       * Hide first label
 136       *
 137       * @var bool
 138       */
 139      protected $hideFirst = FALSE;
 140      
 141      /**
 142       * Hide last label
 143       *
 144       * @var bool
 145       */
 146      protected $hideLast = FALSE;
 147      
 148      /**
 149       * Build the label
 150       *
 151       * @param string $label First label
 152       */
 153  	public function __construct($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($color instanceof 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  	public 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  	public 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  	public 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  	public 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  	public 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  	public 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  	public 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 awFont $font New font
 259       * @param awColor $color Font color (can be NULL)
 260       */
 261  	public function setFont(awFont $font, $color = NULL) {
 262          $this->font = $font;
 263          if($color instanceof awColor) {
 264              $this->setColor($color);
 265          }
 266      }
 267      
 268      /**
 269       * Change font angle
 270       *
 271       * @param int $angle New angle
 272       */
 273  	public function setAngle($angle) {
 274          $this->angle = (int)$angle;
 275      }
 276      
 277      /**
 278       * Change font color
 279       *
 280       * @param awColor $color
 281       */
 282  	public function setColor($color) {
 283          $this->color = $color;
 284      }
 285      
 286      /**
 287       * Change text background
 288       *
 289       * @param mixed $background
 290       */
 291  	public function setBackground($background) {
 292          $this->background = $background;
 293      }
 294      
 295      /**
 296       * Change text background color
 297       *
 298       * @param Color
 299       */
 300  	public function setBackgroundColor(awColor $color) {
 301          $this->background = $color;
 302      }
 303      
 304      /**
 305       * Change text background gradient
 306       *
 307       * @param Gradient
 308       */
 309  	public function setBackgroundGradient(awGradient $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  	public 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  	public function hide($hide = TRUE) {
 331          $this->hide = (bool)$hide;
 332      }
 333      
 334      /**
 335       * Show all labels ?
 336       *
 337       * @param bool $show
 338       */
 339  	public 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  	public 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  	public function hideValue($value) {
 358          $this->hideValue[] = $value;
 359      }
 360      
 361      /**
 362       * Hide first label
 363       *
 364       * @param bool $hide
 365       */
 366  	public function hideFirst($hide) {
 367          $this->hideFirst = (bool)$hide;
 368      }
 369      
 370      /**
 371       * Hide last label
 372       *
 373       * @param bool $hide
 374       */
 375  	public function hideLast($hide) {
 376          $this->hideLast = (bool)$hide;
 377      }
 378      
 379      /**
 380       * Set label interval
 381       *
 382       * @param int
 383       */
 384  	public 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  	public 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  	public 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  	public 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($this->background instanceof awColor) {
 439                  $text->setBackgroundColor($this->background);
 440              } else if($this->background instanceof 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 awDrawer $drawer A drawer
 462       * @return int
 463       */
 464  	public function getMaxWidth(awDrawer $drawer) {
 465      
 466          return $this->getMax($drawer, 'getTextWidth');
 467      
 468      }
 469      
 470      /**
 471       * Get max height of all texts
 472       *
 473       * @param awDrawer $drawer A drawer
 474       * @return int
 475       */
 476  	public function getMaxHeight(awDrawer $drawer) {
 477      
 478          return $this->getMax($drawer, 'getTextHeight');
 479          
 480      }
 481      
 482      /**
 483       * Draw the label
 484       *
 485       * @param awDrawer $drawer
 486       * @param awPoint $p Label center
 487       * @param int $key Text position in the array of texts (default to zero)
 488       */
 489  	public function draw(awDrawer $drawer, awPoint $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              // Value must be hidden
 519              if(in_array($text->getText(), $this->hideValue)) {
 520                  return;
 521              }
 522          
 523              $x = $p->x;
 524              $y = $p->y;
 525              
 526              // Get padding
 527              list($left, $right, $top, $bottom) = $text->getPadding();
 528              
 529              $font = $text->getFont();
 530              $width = $font->getTextWidth($text);
 531              $height = $font->getTextHeight($text);
 532              
 533              switch($this->hAlign) {
 534              
 535                  case awLabel::RIGHT :
 536                      $x -= ($width + $right);
 537                      break;
 538              
 539                  case awLabel::CENTER :
 540                      $x -= ($width - $left + $right) / 2;
 541                      break;
 542              
 543                  case awLabel::LEFT :
 544                      $x += $left;
 545                      break;
 546              
 547              }
 548              
 549              switch($this->vAlign) {
 550              
 551                  case awLabel::TOP :
 552                      $y -= ($height + $bottom);
 553                      break;
 554              
 555                  case awLabel::MIDDLE :
 556                      $y -= ($height - $top + $bottom) / 2;
 557                      break;
 558              
 559                  case awLabel::BOTTOM :
 560                      $y += $top;
 561                      break;
 562              
 563              }
 564          
 565              $drawer->string($text, $this->move->move($x, $y));
 566              
 567          }
 568          
 569      }
 570      
 571  	protected function getMax(awDrawer $drawer, $function) {
 572      
 573          $max = NULL;
 574      
 575          foreach($this->texts as $key => $text) {
 576          
 577              $text = $this->getText($key);
 578              $font = $text->getFont();
 579          
 580              if(is_null($max)) {
 581                  $max = $font->{$function}($text);
 582              } else {
 583                  $max = max($max, $font->{$function}($text));
 584              }
 585          
 586          }
 587          
 588          return $max;
 589          
 590      }
 591  
 592  }
 593  
 594  registerClass('Label');
 595  ?>


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