[ 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/ -> Drawer.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   * Draw your objects
  12   *
  13   * @package Artichow
  14   */
  15  class awDrawer {
  16  
  17      /**
  18       * A GD resource
  19       *
  20       * @var $resource
  21       */
  22      var $resource;
  23      
  24      /**
  25       * Image width
  26       *
  27       * @var int
  28       */
  29      var $width;
  30      
  31      /**
  32       * Image height
  33       *
  34       * @var int
  35       */
  36      var $height;
  37      
  38      /**
  39       * Drawer X position
  40       *
  41       * @var int
  42       */
  43      var $x;
  44      
  45      /**
  46       * Drawer Y position
  47       *
  48       * @var int
  49       */
  50      var $y;
  51      
  52      var $w;
  53      var $h;
  54  
  55      /**
  56       * Build your drawer
  57       *
  58       * @var resource $resource A GD resource
  59       */
  60  	 function awDrawer($resource) {
  61      
  62          $this->resource = $resource;
  63          
  64      }
  65      
  66      /**
  67       * Change the image size
  68       *
  69       * @param int $width Image width
  70       * @param int $height Image height
  71       */
  72  	 function setImageSize($width, $height) {
  73      
  74          $this->width = $width;
  75          $this->height = $height;
  76      
  77      }
  78      
  79      /**
  80       * Inform the drawer of the position of your image
  81       *
  82       * @param float $x Position on X axis of the center of the component
  83       * @param float $y Position on Y axis of the center of the component
  84       */
  85  	 function setPosition($x, $y) {
  86          
  87          // Calcul absolute position
  88          $this->x = round($x * $this->width - $this->w / 2);
  89          $this->y = round($y * $this->height - $this->h / 2);
  90      
  91      }
  92      
  93      /**
  94       * Inform the drawer of the position of your image
  95       * This method need absolutes values
  96       * 
  97       * @param int $x Left-top corner X position
  98       * @param int $y Left-top corner Y position
  99       */
 100  	 function setAbsPosition($x, $y) {
 101          
 102          $this->x = $x;
 103          $this->y = $y;
 104      
 105      }
 106      
 107      /**
 108       * Move the position of the image
 109       *
 110       * @param int $x Add this value to X axis
 111       * @param int $y Add this value to Y axis
 112       */
 113  	 function movePosition($x, $y) {
 114  
 115          $this->x += (int)$x;
 116          $this->y += (int)$y;
 117      
 118      }
 119      
 120      /**
 121       * Inform the drawer of the size of your image
 122       * Height and width must be between 0 and 1.
 123       *
 124       * @param int $w Image width
 125       * @param int $h Image height
 126       * @return array Absolute width and height of the image
 127       */
 128  	 function setSize($w, $h) {
 129      
 130          // Calcul absolute size
 131          $this->w = round($w * $this->width);
 132          $this->h = round($h * $this->height);
 133          
 134          return $this->getSize();
 135      
 136      }
 137      
 138      /**
 139       * Inform the drawer of the size of your image
 140       * You can set absolute size with this method.
 141       *
 142       * @param int $w Image width
 143       * @param int $h Image height
 144       */
 145  	 function setAbsSize($w, $h) {
 146      
 147          $this->w = $w;
 148          $this->h = $h;
 149          
 150          return $this->getSize();
 151      
 152      }
 153      
 154      /**
 155       * Get the size of the component handled by the drawer
 156       *
 157       * @return array Absolute width and height of the component
 158       */
 159  	 function getSize() {
 160          
 161          return array($this->w, $this->h);
 162      
 163      }
 164      
 165      /**
 166       * Draw an image here
 167       *
 168       * @param &$image Image
 169       * @param int $p1 Image top-left point
 170       * @param int $p2 Image bottom-right point
 171       */
 172  	 function copyImage(&$image, $p1, $p2) {
 173      
 174          list($x1, $y1) = $p1->getLocation();
 175          list($x2, $y2) = $p2->getLocation();
 176      
 177          $drawer = $image->getDrawer();
 178          imagecopy($this->resource, $drawer->resource, $this->x + $x1, $this->y + $y1, 0, 0, $x2 - $x1, $y2 - $y1);
 179      
 180      }
 181      
 182      /**
 183       * Draw an image here
 184       *
 185       * @param &$image Image
 186       * @param int $d1 Destination top-left position
 187       * @param int $d2 Destination bottom-right position
 188       * @param int $s1 Source top-left position
 189       * @param int $s2 Source bottom-right position
 190       * @param bool $resample Resample image ? (default to TRUE)
 191       */
 192  	 function copyResizeImage(&$image, $d1, $d2, $s1, $s2, $resample = TRUE) {
 193          
 194          if($resample) {
 195              $function = 'imagecopyresampled';
 196          } else {
 197              $function = 'imagecopyresized';
 198          }
 199          
 200          $drawer = $image->getDrawer();
 201      
 202          $function(
 203              $this->resource,
 204              $drawer->resource,
 205              $this->x + $d1->x, $this->y + $d1->y,
 206              $s1->x, $s1->y,
 207              $d2->x - $d1->x, $d2->y - $d1->y,
 208              $s2->x - $s1->x, $s2->y - $s1->y
 209          );
 210      
 211      }
 212      
 213      /**
 214       * Draw a string
 215       *
 216       * @var &$text Text to print
 217       * @param $point Draw the text at this point
 218       */
 219  	 function string(&$text, $point) {
 220          
 221          $font = $text->getFont();
 222          
 223          if($text->getBackground() !== NULL or $text->border->visible()) {
 224          
 225              list($left, $right, $top, $bottom) = $text->getPadding();
 226              
 227              $width = $font->getTextWidth($text);
 228              $height = $font->getTextHeight($text);
 229              
 230              $x1 = floor($point->x - $left);
 231              $y1 = floor($point->y - $top);
 232              $x2 = $x1 + $width + $left + $right;
 233              $y2 = $y1 + $height + $top + $bottom;
 234              
 235              $this->filledRectangle(
 236                  $text->getBackground(),
 237                  awLine::build($x1, $y1, $x2, $y2)
 238              );
 239              
 240              $text->border->rectangle(
 241                  $this,
 242                  new awPoint($x1 - 1, $y1 - 1),
 243                  new awPoint($x2 + 1, $y2 + 1)
 244              );
 245              
 246          }
 247          
 248          $font->draw($this, $point, $text);
 249          
 250      }
 251      
 252      /**
 253       * Draw a pixel
 254       *
 255       * @param $color Pixel color
 256       * @param $p
 257       */
 258  	 function point($color, $p) {
 259      
 260          if($p->isHidden() === FALSE) {
 261              $rgb = $color->getColor($this->resource);
 262              imagesetpixel($this->resource, $this->x + round($p->x), $this->y + round($p->y), $rgb);
 263          }
 264      
 265      }
 266      
 267      /**
 268       * Draw a colored line
 269       *
 270       * @param $color Line color
 271       * @param $line
 272       * @param int $thickness Line tickness
 273       */
 274  	 function line($color, $line) {
 275      
 276          if($line->thickness > 0 and $line->isHidden() === FALSE) {
 277      
 278              $rgb = $color->getColor($this->resource);
 279              $thickness = $line->thickness;
 280              
 281              list($p1, $p2) = $line->getLocation();
 282              
 283              $this->startThickness($thickness);
 284              
 285              switch($line->getStyle()) {
 286              
 287                  case LINE_SOLID :
 288                      imageline($this->resource, $this->x + round($p1->x), $this->y + round($p1->y), $this->x + round($p2->x), $this->y + round($p2->y), $rgb);
 289                      break;
 290                      
 291                  case LINE_DOTTED :
 292                      $size = sqrt(pow($p2->y - $p1->y, 2) + pow($p2->x - $p1->x, 2));
 293                      $cos = ($p2->x - $p1->x) / $size;
 294                      $sin = ($p2->y - $p1->y) / $size;
 295                      for($i = 0; $i <= $size; $i += 2) {
 296                          $p = new awPoint(
 297                              round($i * $cos + $p1->x),
 298                              round($i * $sin + $p1->y)
 299                          );
 300                          $this->point($color, $p);
 301                      }
 302                      break;
 303                      
 304                  case LINE_DASHED :
 305                      $width = $p2->x - $p1->x;
 306                      $height = $p2->y - $p1->y;
 307                      $size = sqrt(pow($height, 2) + pow($width, 2));
 308                      
 309                      if($size == 0) {
 310                          return;
 311                      }
 312                      
 313                      $cos = $width / $size;
 314                      $sin = $height / $size;
 315                      
 316                      for($i = 0; $i <= $size; $i += 6) {
 317                          
 318                          $t1 = new awPoint(
 319                              round($i * $cos + $p1->x),
 320                              round($i * $sin + $p1->y)
 321                          );
 322                          
 323                          $function = ($height > 0) ? 'min' : 'max';
 324                          $t2 = new awPoint(
 325                              round(min(($i + 3) * $cos, $width) + $p1->x),
 326                              round($function(($i + 3) * $sin, $height) + $p1->y)
 327                          );
 328                          
 329                          $this->line($color, new awLine($t1, $t2));
 330                          
 331                      }
 332                      break;
 333              
 334              }
 335              
 336              $this->stopThickness($thickness);
 337              
 338          }
 339          
 340      }
 341      
 342      /**
 343       * Draw a color arc
 344       
 345       * @param $color Arc color
 346       * @param $center Point center
 347       * @param int $width Ellipse width
 348       * @param int $height Ellipse height
 349       * @param int $from Start angle
 350       * @param int $to End angle
 351       */
 352  	 function arc($color, $center, $width, $height, $from, $to) {
 353      
 354          imagefilledarc(
 355              $this->resource,
 356              $this->x + $center->x, $this->y + $center->y,
 357              $width, $height,
 358              $from, $to,
 359              $color->getColor($this->resource),
 360              IMG_ARC_EDGED | IMG_ARC_NOFILL
 361          );
 362      
 363      }
 364      
 365      /**
 366       * Draw an arc with a background color
 367       *
 368       * @param $color Arc background color
 369       * @param $center Point center
 370       * @param int $width Ellipse width
 371       * @param int $height Ellipse height
 372       * @param int $from Start angle
 373       * @param int $to End angle
 374       */
 375  	 function filledArc($color, $center, $width, $height, $from, $to) {
 376      
 377          imagefilledarc(
 378              $this->resource,
 379              $this->x + $center->x, $this->y + $center->y,
 380              $width, $height,
 381              $from, $to,
 382              $color->getColor($this->resource),
 383              IMG_ARC_PIE
 384          );
 385      
 386      }
 387      
 388      /**
 389       * Draw a colored ellipse
 390       *
 391       * @param $color Ellipse color
 392       * @param $center Ellipse center
 393       * @param int $width Ellipse width
 394       * @param int $height Ellipse height
 395       */
 396  	 function ellipse($color, $center, $width, $height) {
 397      
 398          list($x, $y) = $center->getLocation();
 399      
 400          $rgb = $color->getColor($this->resource);
 401          imageellipse(
 402              $this->resource,
 403              $this->x + $x,
 404              $this->y + $y,
 405              $width,
 406              $height,
 407              $rgb
 408          );
 409          
 410      }
 411      
 412      /**
 413       * Draw an ellipse with a background
 414       *
 415       * @param mixed $background Background (can be a color or a gradient)
 416       * @param $center Ellipse center
 417       * @param int $width Ellipse width
 418       * @param int $height Ellipse height
 419       */
 420  	 function filledEllipse($background, $center, $width, $height) {
 421      
 422          if(is_a($background, 'awColor')) {
 423      
 424              list($x, $y) = $center->getLocation();
 425          
 426              $rgb = $background->getColor($this->resource);
 427              
 428              imagefilledellipse(
 429                  $this->resource,
 430                  $this->x + $x,
 431                  $this->y + $y,
 432                  $width,
 433                  $height,
 434                  $rgb
 435              );
 436              
 437          } else if(is_a($background, 'awGradient')) {
 438      
 439              list($x, $y) = $center->getLocation();
 440              
 441              $x1 = $x - round($width / 2);
 442              $y1 = $y - round($height / 2);
 443              $x2 = $x1 + $width;
 444              $y2 = $y1 + $height;
 445          
 446              $gradientDrawer = new awGradientDrawer($this);
 447              $gradientDrawer->filledEllipse(
 448                  $background,
 449                  $x1, $y1,
 450                  $x2, $y2
 451              );
 452          
 453          }
 454          
 455      }
 456      
 457      /**
 458       * Draw a colored rectangle
 459       *
 460       * @param $color Rectangle color
 461       * @param $line Rectangle diagonale
 462       * @param $p2
 463       */
 464  	 function rectangle($color, $line) {
 465      
 466          $p1 = $line->p1;
 467          $p2 = $line->p2;
 468          
 469          switch($line->getStyle()) {
 470          
 471              case LINE_SOLID :
 472                  $thickness = $line->getThickness();
 473                  $this->startThickness($thickness);
 474                  $rgb = $color->getColor($this->resource);
 475                  imagerectangle($this->resource, $this->x + $p1->x, $this->y + $p1->y, $this->x + $p2->x, $this->y + $p2->y, $rgb);
 476                  $this->stopThickness($thickness);
 477                  break;
 478              
 479              default :
 480                  
 481                  // Top side
 482                  $line->setLocation(
 483                      new awPoint($p1->x, $p1->y),
 484                      new awPoint($p2->x, $p1->y)
 485                  );
 486                  $this->line($color, $line);
 487                  
 488                  // Right side
 489                  $line->setLocation(
 490                      new awPoint($p2->x, $p1->y),
 491                      new awPoint($p2->x, $p2->y)
 492                  );
 493                  $this->line($color, $line);
 494                  
 495                  // Bottom side
 496                  $line->setLocation(
 497                      new awPoint($p1->x, $p2->y),
 498                      new awPoint($p2->x, $p2->y)
 499                  );
 500                  $this->line($color, $line);
 501                  
 502                  // Left side
 503                  $line->setLocation(
 504                      new awPoint($p1->x, $p1->y),
 505                      new awPoint($p1->x, $p2->y)
 506                  );
 507                  $this->line($color, $line);
 508              
 509                  break;
 510          
 511          }
 512      
 513      }
 514      
 515      /**
 516       * Draw a rectangle with a background
 517       *
 518       * @param mixed $background Background (can be a color or a gradient)
 519       * @param $line Rectangle diagonale
 520       */
 521  	 function filledRectangle($background, $line) {
 522      
 523          $p1 = $line->p1;
 524          $p2 = $line->p2;
 525      
 526          if(is_a($background, 'awColor')) {
 527              $rgb = $background->getColor($this->resource);
 528              imagefilledrectangle($this->resource, $this->x + $p1->x, $this->y + $p1->y, $this->x + $p2->x, $this->y + $p2->y, $rgb);
 529          } else if(is_a($background, 'awGradient')) {
 530              $gradientDrawer = new awGradientDrawer($this);
 531              $gradientDrawer->filledRectangle($background, $p1, $p2);
 532          }
 533      
 534      }
 535      
 536      /**
 537       * Draw a polygon
 538       *
 539       * @param $color Polygon color
 540       * @param Polygon A polygon
 541       */
 542  	 function polygon($color, &$polygon) {
 543          
 544          switch($polygon->getStyle()) {
 545          
 546              case POLYGON_SOLID :
 547                  $thickness = $line->getThickness();
 548                  $this->startThickness($thickness);
 549                  $points = $this->getPolygonPoints($polygon);
 550                  $rgb = $color->getColor($this->resource);
 551                  imagepolygon($this->resource, $points, $polygon->count(), $rgb);
 552                  $this->stopThickness($thickness);
 553                  break;
 554                  
 555              default :
 556              
 557                  if($polygon->count() > 1) {
 558                  
 559                      $prev = $polygon->get(0);
 560                      
 561                      $line = new awLine;
 562                      $line->setStyle($polygon->getStyle());
 563                      $line->setThickness($polygon->getThickness());
 564                      
 565                      for($i = 1; $i < $polygon->count(); $i++) {
 566                          $current = $polygon->get($i);
 567                          $line->setLocation($prev, $current);
 568                          $this->line($color, $line);
 569                          $prev = $current;
 570                      }
 571                      
 572                  }
 573          
 574          }
 575          
 576      }
 577      
 578      /**
 579       * Draw a polygon with a background
 580       *
 581       * @param mixed $background Background (can be a color or a gradient)
 582       * @param Polygon A polygon
 583       */
 584  	 function filledPolygon($background, &$polygon) {
 585          
 586          if(is_a($background, 'awColor')) {
 587              $points = $this->getPolygonPoints($polygon);
 588              $rgb = $background->getColor($this->resource);
 589              imagefilledpolygon($this->resource, $points, $polygon->count(), $rgb);
 590          } else if(is_a($background, 'awGradient')) {
 591              $gradientDrawer = new awGradientDrawer($this);
 592              $gradientDrawer->filledPolygon($background, $polygon);
 593          }
 594          
 595      }
 596      
 597  	 function getPolygonPoints(&$polygon) {
 598          
 599          $points = array();
 600          
 601          foreach($polygon->all() as $point) {
 602              $points[] = $point->x + $this->x;
 603              $points[] = $point->y + $this->y;
 604          }
 605          
 606          return $points;
 607          
 608      }
 609      
 610  	 function startThickness($thickness) {
 611          
 612          if($thickness > 1) {
 613          
 614              // Beurk :'(
 615               if(function_exists('imageantialias')) 
 616              imageantialias($this->resource, FALSE);
 617              imagesetthickness($this->resource, $thickness);
 618              
 619          }
 620          
 621      }
 622      
 623  	 function stopThickness($thickness) {
 624          
 625          if($thickness > 1) {
 626          
 627              imagesetthickness($this->resource, 1);
 628               if(function_exists('imageantialias'))
 629              imageantialias($this->resource, TRUE);
 630              
 631          }
 632          
 633      }
 634      
 635  
 636  }
 637  
 638  registerClass('Drawer');
 639  
 640  /**
 641   * To your gradients
 642   *
 643   * @package Artichow
 644   */
 645  
 646  class awGradientDrawer {
 647  
 648      /**
 649       * A drawer
 650       *
 651       * @var Drawer
 652       */
 653      var $drawer;
 654  
 655      /**
 656       * Build your GradientDrawer
 657       *
 658       * @var $drawer 
 659       */
 660  	 function awGradientDrawer($drawer) {
 661      
 662          $this->drawer = $drawer;
 663          
 664      }
 665      
 666  	 function drawFilledFlatTriangle($gradient, $a, $b, $c) {
 667      
 668          if($gradient->angle !== 0) {
 669              trigger_error("Flat triangles can only be used with 0 degree gradients", E_USER_ERROR);
 670          }
 671      
 672          // Look for right-angled triangle
 673          if($a->x !== $b->x and $b->x !== $c->x) {
 674              trigger_error("Not right-angled flat triangles are not supported yet", E_USER_ERROR);
 675          }
 676          
 677          if($a->x === $b->x) {
 678              $d = $a;
 679              $e = $c;
 680          } else {
 681              $d = $c;
 682              $e = $a;
 683          }
 684          
 685          $this->init($gradient, $b->y - $d->y);
 686      
 687          for($i = $c->y + 1; $i < $b->y; $i++) {
 688              
 689              $color = $this->color($i - $d->y);
 690              $pos = ($i - $d->y) / ($b->y - $d->y);
 691              
 692              $p1 = new awPoint($e->x, $i);
 693              $p2 = new awPoint(1 + floor($e->x - $pos * ($e->x - $d->x)), $i);
 694              
 695              $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 696              
 697              $color->free();
 698              unset($color);
 699              
 700          }
 701      
 702      }
 703      
 704  	 function filledRectangle($gradient, $p1, $p2) {
 705      
 706          list($x1, $y1) = $p1->getLocation();
 707          list($x2, $y2) = $p2->getLocation();
 708      
 709          if($y1 < $y2) {
 710              $y1 ^= $y2 ^= $y1 ^= $y2;
 711          }
 712      
 713          if($x2 < $x1) {
 714              $x1 ^= $x2 ^= $x1 ^= $x2;
 715          }
 716          
 717          if(is_a($gradient, 'awLinearGradient')) {
 718              $this->rectangleLinearGradient($gradient, new awPoint($x1, $y1), new awPoint($x2, $y2));
 719          } else {
 720              trigger_error("This gradient is not supported by rectangles", E_USER_ERROR);
 721          }
 722      
 723      }
 724      
 725  	 function filledPolygon($gradient, &$polygon) {
 726      
 727          if(is_a($gradient, 'awLinearGradient')) {
 728              $this->polygonLinearGradient($gradient, $polygon);
 729          } else {
 730              trigger_error("This gradient is not supported by polygons", E_USER_ERROR);
 731          }
 732      
 733      }
 734      
 735  	 function rectangleLinearGradient(&$gradient, $p1, $p2) {
 736      
 737          list($x1, $y1) = $p1->getLocation();
 738          list($x2, $y2) = $p2->getLocation();
 739      
 740          if($y1 - $y2 > 0) {
 741          
 742              if($gradient->angle === 0) {
 743              
 744                  $this->init($gradient, $y1 - $y2);
 745          
 746                  for($i = $y2; $i <= $y1; $i++) {
 747                  
 748                      $color = $this->color($i - $y2);
 749                      
 750                      $p1 = new awPoint($x1, $i);
 751                      $p2 = new awPoint($x2, $i);
 752              
 753                      $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 754                      
 755                      $color->free();
 756                      unset($color);
 757                      
 758                  }
 759                  
 760              } else if($gradient->angle === 90) {
 761              
 762                  $this->init($gradient, $x2 - $x1);
 763          
 764                  for($i = $x1; $i <= $x2; $i++) {
 765                  
 766                      $color = $this->color($i - $x1);
 767                      
 768                      $p1 = new awPoint($i, $y2);
 769                      $p2 = new awPoint($i, $y1);
 770              
 771                      $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 772                      
 773                      $color->free();
 774                      unset($color);
 775                      
 776                  }
 777                  
 778              }
 779              
 780          }
 781      
 782      }
 783      
 784  	 function filledEllipse($gradient, $x1, $y1, $x2, $y2) {
 785      
 786          if($y1 < $y2) {
 787              $y1 ^= $y2 ^= $y1 ^= $y2;
 788          }
 789      
 790          if($x2 < $x1) {
 791              $x1 ^= $x2 ^= $x1 ^= $x2;
 792          }
 793          
 794          if(is_a($gradient, 'awRadialGradient')) {
 795              $this->ellipseRadialGradient($gradient, $x1, $y1, $x2, $y2);
 796          } else if(is_a($gradient, 'awLinearGradient')) {
 797              $this->ellipseLinearGradient($gradient, $x1, $y1, $x2, $y2);
 798          } else {
 799              trigger_error("This gradient is not supported by ellipses", E_USER_ERROR);
 800          }
 801      
 802      }
 803      
 804  	 function ellipseRadialGradient($gradient, $x1, $y1, $x2, $y2) {
 805      
 806          if($y1 - $y2 > 0) {
 807      
 808              if($y1 - $y2 != $x2 - $x1) {
 809                  trigger_error("Radial gradients are only implemented on circle, not ellipses");
 810              }
 811              
 812              $c = new awPoint($x1 + ($x2 - $x1) / 2, $y1 + ($y2 - $y1) / 2); 
 813              $r = ($x2 - $x1) / 2;
 814              $ok = array();
 815              
 816              // Init gradient
 817              $this->init($gradient, $r);
 818              
 819              for($i = 0; $i <= $r; $i += 0.45) {
 820              
 821                  $p = ceil((2 * M_PI * $i));
 822                  
 823                  if($p > 0) {
 824                      $interval = 360 / $p;
 825                  } else {
 826                      $interval = 360;
 827                  }
 828                  
 829                  $color = $this->color($i);
 830                  
 831                  for($j = 0; $j < 360; $j += $interval) {
 832                  
 833                      $rad = ($j / 360) * (2 * M_PI);
 834                      
 835                      $x = round($i * cos($rad));
 836                      $y = round($i * sin($rad));
 837                      
 838                      $l = sqrt($x * $x + $y * $y);
 839                      
 840                      if($l <= $r) {
 841                      
 842                          if(
 843                              array_key_exists((int)$x, $ok) === FALSE or
 844                              array_key_exists((int)$y, $ok[$x]) === FALSE
 845                          ) {
 846                          
 847                              // Print the point
 848                              $this->drawer->point($color, new awPoint($c->x + $x, $c->y + $y));
 849                              
 850                              $ok[(int)$x][(int)$y] = TRUE;
 851                          
 852                          }
 853                          
 854                      }
 855                  
 856                  }
 857                  
 858                  $color->free();
 859                  unset($color);
 860              
 861              }
 862          
 863          }
 864      
 865      }
 866      
 867  	 function ellipseLinearGradient($gradient, $x1, $y1, $x2, $y2) {
 868      
 869          // Gauche->droite : 90°
 870      
 871          if($y1 - $y2 > 0) {
 872      
 873              if($y1 - $y2 != $x2 - $x1) {
 874                  trigger_error("Linear gradients are only implemented on circle, not ellipses");
 875              }
 876              
 877              $r = ($x2 - $x1) / 2;
 878              
 879              // Init gradient
 880              $this->init($gradient, $x2 - $x1);
 881              
 882              for($i = -$r; $i <= $r; $i++) {
 883                  
 884                  $h = sin(acos($i / $r)) * $r;
 885                  
 886                  $color = $this->color($i + $r);
 887                  
 888                  if($gradient->angle === 90) {
 889                  
 890                      // Print the line
 891                      $p1 = new awPoint(
 892                          $x1 + $i + $r,
 893                          round(max($y2 + $r - $h + 1, $y2))
 894                      );
 895                      
 896                      $p2 = new awPoint(
 897                          $x1 + $i + $r,
 898                          round(min($y1 - $r + $h - 1, $y1))
 899                      );
 900                      
 901                  } else {
 902                  
 903                      // Print the line
 904                      $p1 = new awPoint(
 905                          round(max($x1 + $r - $h + 1, $x1)),
 906                          $y2 + $i + $r
 907                      );
 908                      
 909                      $p2 = new awPoint(
 910                          round(min($x2 - $r + $h - 1, $x2)),
 911                          $y2 + $i + $r
 912                      );
 913                      
 914                  }
 915                  
 916                  $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 917                  
 918                  $color->free();
 919                  unset($color);
 920              
 921              }
 922          
 923          }
 924      
 925      }
 926      
 927  	 function polygonLinearGradient(&$gradient, &$polygon) {
 928      
 929          $count = $polygon->count();
 930          
 931          if($count >= 3) {
 932          
 933              $left = $polygon->get(0);
 934              $right = $polygon->get($count - 1);
 935              
 936              if($gradient->angle === 0) {
 937              
 938                  // Get polygon maximum and minimum
 939                  $offset = $polygon->get(0);
 940                  $max = $min = $offset->y;
 941                  for($i = 1; $i < $count - 1; $i++) {
 942                      $offset = $polygon->get($i);
 943                      $max = max($max, $offset->y);
 944                      $min = min($min, $offset->y);
 945                  }
 946                  
 947                  $this->init($gradient, $max - $min);
 948              
 949                  $prev = $polygon->get(1);
 950                  
 951                  $sum = 0;
 952              
 953                  for($i = 2; $i < $count - 1; $i++) {
 954                  
 955                      $current = $polygon->get($i);
 956                      
 957                      $interval = 1;
 958                      
 959                      if($i !== $count - 2) {
 960                          $current->x -= $interval;
 961                      }
 962                      
 963                      if($current->x - $prev->x > 0) {
 964                  
 965                          // Draw rectangle
 966                          $x1 = $prev->x;
 967                          $x2 = $current->x;
 968                          $y1 = max($prev->y, $current->y);
 969                          $y2 = $left->y;
 970                          
 971                          $gradient = new awLinearGradient(
 972                              $this->color($max - $min - ($y2 - $y1)),
 973                              $this->color($max - $min),
 974                              0
 975                          );
 976                          
 977                          if($y1 > $y2) {
 978                              $y2 = $y1;
 979                          }
 980                          
 981                          $this->drawer->filledRectangle(
 982                              $gradient,
 983                              awLine::build($x1, $y1, $x2, $y2)
 984                          );
 985                          
 986                          $top = ($prev->y < $current->y) ? $current : $prev;
 987                          $bottom = ($prev->y >= $current->y) ? $current : $prev;
 988                          
 989                          $gradient = new awLinearGradient(
 990                              $this->color($bottom->y - $min),
 991                              $this->color($max - $min - ($y2 - $y1)),
 992                              0
 993                          );
 994                          
 995      
 996                          $gradientDrawer = new awGradientDrawer($this->drawer);
 997                          $gradientDrawer->drawFilledFlatTriangle(
 998                              $gradient,
 999                              new awPoint($prev->x, min($prev->y, $current->y)),
1000                              $top,
1001                              new awPoint($current->x, min($prev->y, $current->y))
1002                          );
1003                          unset($gradientDrawer);
1004                          
1005                          $sum += $current->x - $prev->x;
1006                          
1007                      }
1008                      
1009                      $prev = $current;
1010                      $prev->x += $interval;
1011                  
1012                  }
1013              
1014              } else if($gradient->angle === 90) {
1015                  
1016                  $width = $right->x - $left->x;
1017                  $this->init($gradient, $width);
1018                  
1019                  $pos = 1;
1020                  $next = $polygon->get($pos++);
1021                  
1022                  $this->next($polygon, $pos, $prev, $next);
1023              
1024                  for($i = 0; $i <= $width; $i++) {
1025                  
1026                      $x = $left->x + $i;
1027                      
1028                      $y1 = round($prev->y + ($next->y - $prev->y) * (($i + $left->x - $prev->x) / ($next->x - $prev->x)));
1029                      $y2 = $left->y;
1030                  
1031                      // Draw line
1032                      $color = $this->color($i);
1033                      // YaPB : PHP does not handle alpha on lines
1034                      $this->drawer->filledRectangle($color, awLine::build($x, $y1, $x, $y2));
1035                      $color->free();
1036                      unset($color);
1037                      
1038                      // Jump to next point
1039                      if($next->x == $i + $left->x) {
1040                      
1041                          $this->next($polygon, $pos, $prev, $next);
1042                          
1043                      }
1044                  
1045                  }
1046      
1047              }
1048              
1049          }
1050      
1051      }
1052      
1053  	 function next($polygon, &$pos, &$prev, &$next) {
1054      
1055          do {
1056              $prev = $next;
1057              $next = $polygon->get($pos++);
1058          }
1059          while($next->x - $prev->x == 0 and $pos < $polygon->count());
1060          
1061      }
1062      
1063      /**
1064       * Start colors
1065       *
1066       * @var int
1067       */
1068      var $r1, $g1, $b1, $a1;
1069      
1070      /**
1071       * Stop colors
1072       *
1073       * @var int
1074       */
1075      var $r2, $g2, $b2, $a2;
1076      
1077      /**
1078       * Gradient size in pixels
1079       *
1080       * @var int
1081       */
1082      var $size;
1083      
1084      
1085  	 function init($gradient, $size) {
1086          
1087          list(
1088              $this->r1, $this->g1, $this->b1, $this->a1
1089          ) = $gradient->from->rgba();
1090          
1091          list(
1092              $this->r2, $this->g2, $this->b2, $this->a2
1093          ) = $gradient->to->rgba();
1094          
1095          $this->size = $size;
1096      }
1097      
1098  	 function color($pos) {
1099      
1100          return new awColor(
1101              $this->getRed($pos),
1102              $this->getGreen($pos),
1103              $this->getBlue($pos),
1104              $this->getAlpha($pos)
1105          );
1106          
1107      }
1108      
1109      
1110  	 function getRed($pos) {
1111          return (int)round($this->r1 + ($pos / $this->size) * ($this->r2 - $this->r1));
1112      }
1113      
1114  	 function getGreen($pos) {
1115          return (int)round($this->g1 + ($pos / $this->size) * ($this->g2 - $this->g1));
1116      }
1117      
1118  	 function getBlue($pos) {
1119          return (int)round($this->b1 + ($pos / $this->size) * ($this->b2 - $this->b1));
1120      }
1121      
1122  	 function getAlpha($pos) {
1123          return (int)round(($this->a1 + ($pos / $this->size) * ($this->a2 - $this->a1)) / 127 * 100);
1124      }
1125  
1126  }
1127  
1128  registerClass('GradientDrawer');
1129  ?>


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