[ 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/ -> 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      public $resource;
  23      
  24      /**
  25       * Image width
  26       *
  27       * @var int
  28       */
  29      public $width;
  30      
  31      /**
  32       * Image height
  33       *
  34       * @var int
  35       */
  36      public $height;
  37      
  38      /**
  39       * Drawer X position
  40       *
  41       * @var int
  42       */
  43      public $x;
  44      
  45      /**
  46       * Drawer Y position
  47       *
  48       * @var int
  49       */
  50      public $y;
  51      
  52      private $w;
  53      private $h;
  54  
  55      /**
  56       * Build your drawer
  57       *
  58       * @var resource $resource A GD resource
  59       */
  60  	public function __construct($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  	public 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  	public 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  	public 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  	public 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  	public 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  	public 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  	public function getSize() {
 160          
 161          return array($this->w, $this->h);
 162      
 163      }
 164      
 165      /**
 166       * Draw an image here
 167       *
 168       * @param awImage $image Image
 169       * @param int $p1 Image top-left point
 170       * @param int $p2 Image bottom-right point
 171       */
 172  	public function copyImage(awImage $image, awPoint $p1, awPoint $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 awImage $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  	public function copyResizeImage(awImage $image, awPoint $d1, awPoint $d2, awPoint $s1, awPoint $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 awText $text Text to print
 217       * @param awPoint $point Draw the text at this point
 218       */
 219  	public function string(awText $text, awPoint $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 awColor $color Pixel color
 256       * @param awPoint $p
 257       */
 258  	public function point(awColor $color, awPoint $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 awColor $color Line color
 271       * @param awLine $line
 272       * @param int $thickness Line tickness
 273       */
 274  	public function line(awColor $color, awLine $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 awLine::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 awLine::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 awLine::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 awColor $color Arc color
 346       * @param awPoint $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  	public function arc(awColor $color, awPoint $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 awColor $color Arc background color
 369       * @param awPoint $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  	public function filledArc(awColor $color, awPoint $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 awColor $color Ellipse color
 392       * @param awPoint $center Ellipse center
 393       * @param int $width Ellipse width
 394       * @param int $height Ellipse height
 395       */
 396  	public function ellipse(awColor $color, awPoint $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 awPoint $center Ellipse center
 417       * @param int $width Ellipse width
 418       * @param int $height Ellipse height
 419       */
 420  	public function filledEllipse($background, awPoint $center, $width, $height) {
 421      
 422          if($background instanceof 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($background instanceof 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 awColor $color Rectangle color
 461       * @param awLine $line Rectangle diagonale
 462       * @param awPoint $p2
 463       */
 464  	public function rectangle(awColor $color, awLine $line) {
 465      
 466          $p1 = $line->p1;
 467          $p2 = $line->p2;
 468          
 469          switch($line->getStyle()) {
 470          
 471              case awLine::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 awLine $line Rectangle diagonale
 520       */
 521  	public function filledRectangle($background, awLine $line) {
 522      
 523          $p1 = $line->p1;
 524          $p2 = $line->p2;
 525      
 526          if($background instanceof 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($background instanceof awGradient) {
 530              $gradientDrawer = new awGradientDrawer($this);
 531              $gradientDrawer->filledRectangle($background, $p1, $p2);
 532          }
 533      
 534      }
 535      
 536      /**
 537       * Draw a polygon
 538       *
 539       * @param awColor $color Polygon color
 540       * @param Polygon A polygon
 541       */
 542  	public function polygon(awColor $color, awPolygon $polygon) {
 543          
 544          switch($polygon->getStyle()) {
 545          
 546              case awPolygon::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  	public function filledPolygon($background, awPolygon $polygon) {
 585          
 586          if($background instanceof awColor) {
 587              $points = $this->getPolygonPoints($polygon);
 588              $rgb = $background->getColor($this->resource);
 589              imagefilledpolygon($this->resource, $points, $polygon->count(), $rgb);
 590          } else if($background instanceof awGradient) {
 591              $gradientDrawer = new awGradientDrawer($this);
 592              $gradientDrawer->filledPolygon($background, $polygon);
 593          }
 594          
 595      }
 596      
 597  	private function getPolygonPoints(awPolygon $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  	private function startThickness($thickness) {
 611          
 612          if($thickness > 1) {
 613          
 614              // Beurk :'(
 615              imageantialias($this->resource, FALSE);
 616              imagesetthickness($this->resource, $thickness);
 617              
 618          }
 619          
 620      }
 621      
 622  	private function stopThickness($thickness) {
 623          
 624          if($thickness > 1) {
 625          
 626              imagesetthickness($this->resource, 1);
 627              imageantialias($this->resource, TRUE);
 628              
 629          }
 630          
 631      }
 632      
 633  
 634  }
 635  
 636  registerClass('Drawer');
 637  
 638  /**
 639   * To your gradients
 640   *
 641   * @package Artichow
 642   */
 643  
 644  class awGradientDrawer {
 645  
 646      /**
 647       * A drawer
 648       *
 649       * @var Drawer
 650       */
 651      protected $drawer;
 652  
 653      /**
 654       * Build your GradientDrawer
 655       *
 656       * @var awDrawer $drawer 
 657       */
 658  	public function __construct(awDrawer $drawer) {
 659      
 660          $this->drawer = $drawer;
 661          
 662      }
 663      
 664  	public function drawFilledFlatTriangle(awGradient $gradient, awPoint $a, awPoint $b, awPoint $c) {
 665      
 666          if($gradient->angle !== 0) {
 667              trigger_error("Flat triangles can only be used with 0 degree gradients", E_USER_ERROR);
 668          }
 669      
 670          // Look for right-angled triangle
 671          if($a->x !== $b->x and $b->x !== $c->x) {
 672              trigger_error("Not right-angled flat triangles are not supported yet", E_USER_ERROR);
 673          }
 674          
 675          if($a->x === $b->x) {
 676              $d = $a;
 677              $e = $c;
 678          } else {
 679              $d = $c;
 680              $e = $a;
 681          }
 682          
 683          $this->init($gradient, $b->y - $d->y);
 684      
 685          for($i = $c->y + 1; $i < $b->y; $i++) {
 686              
 687              $color = $this->color($i - $d->y);
 688              $pos = ($i - $d->y) / ($b->y - $d->y);
 689              
 690              $p1 = new awPoint($e->x, $i);
 691              $p2 = new awPoint(1 + floor($e->x - $pos * ($e->x - $d->x)), $i);
 692              
 693              $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 694              
 695              $color->free();
 696              unset($color);
 697              
 698          }
 699      
 700      }
 701      
 702  	public function filledRectangle(awGradient $gradient, awPoint $p1, awPoint $p2) {
 703      
 704          list($x1, $y1) = $p1->getLocation();
 705          list($x2, $y2) = $p2->getLocation();
 706      
 707          if($y1 < $y2) {
 708              $y1 ^= $y2 ^= $y1 ^= $y2;
 709          }
 710      
 711          if($x2 < $x1) {
 712              $x1 ^= $x2 ^= $x1 ^= $x2;
 713          }
 714          
 715          if($gradient instanceof awLinearGradient) {
 716              $this->rectangleLinearGradient($gradient, new awPoint($x1, $y1), new awPoint($x2, $y2));
 717          } else {
 718              trigger_error("This gradient is not supported by rectangles", E_USER_ERROR);
 719          }
 720      
 721      }
 722      
 723  	public function filledPolygon(awGradient $gradient, awPolygon $polygon) {
 724      
 725          if($gradient instanceof awLinearGradient) {
 726              $this->polygonLinearGradient($gradient, $polygon);
 727          } else {
 728              trigger_error("This gradient is not supported by polygons", E_USER_ERROR);
 729          }
 730      
 731      }
 732      
 733  	protected function rectangleLinearGradient(awLinearGradient $gradient, awPoint $p1, awPoint $p2) {
 734      
 735          list($x1, $y1) = $p1->getLocation();
 736          list($x2, $y2) = $p2->getLocation();
 737      
 738          if($y1 - $y2 > 0) {
 739          
 740              if($gradient->angle === 0) {
 741              
 742                  $this->init($gradient, $y1 - $y2);
 743          
 744                  for($i = $y2; $i <= $y1; $i++) {
 745                  
 746                      $color = $this->color($i - $y2);
 747                      
 748                      $p1 = new awPoint($x1, $i);
 749                      $p2 = new awPoint($x2, $i);
 750              
 751                      $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 752                      
 753                      $color->free();
 754                      unset($color);
 755                      
 756                  }
 757                  
 758              } else if($gradient->angle === 90) {
 759              
 760                  $this->init($gradient, $x2 - $x1);
 761          
 762                  for($i = $x1; $i <= $x2; $i++) {
 763                  
 764                      $color = $this->color($i - $x1);
 765                      
 766                      $p1 = new awPoint($i, $y2);
 767                      $p2 = new awPoint($i, $y1);
 768              
 769                      $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 770                      
 771                      $color->free();
 772                      unset($color);
 773                      
 774                  }
 775                  
 776              }
 777              
 778          }
 779      
 780      }
 781      
 782  	public function filledEllipse(awGradient $gradient, $x1, $y1, $x2, $y2) {
 783      
 784          if($y1 < $y2) {
 785              $y1 ^= $y2 ^= $y1 ^= $y2;
 786          }
 787      
 788          if($x2 < $x1) {
 789              $x1 ^= $x2 ^= $x1 ^= $x2;
 790          }
 791          
 792          if($gradient instanceof awRadialGradient) {
 793              $this->ellipseRadialGradient($gradient, $x1, $y1, $x2, $y2);
 794          } else if($gradient instanceof awLinearGradient) {
 795              $this->ellipseLinearGradient($gradient, $x1, $y1, $x2, $y2);
 796          } else {
 797              trigger_error("This gradient is not supported by ellipses", E_USER_ERROR);
 798          }
 799      
 800      }
 801      
 802  	protected function ellipseRadialGradient(awGradient $gradient, $x1, $y1, $x2, $y2) {
 803      
 804          if($y1 - $y2 > 0) {
 805      
 806              if($y1 - $y2 != $x2 - $x1) {
 807                  trigger_error("Radial gradients are only implemented on circle, not ellipses");
 808              }
 809              
 810              $c = new awPoint($x1 + ($x2 - $x1) / 2, $y1 + ($y2 - $y1) / 2); 
 811              $r = ($x2 - $x1) / 2;
 812              $ok = array();
 813              
 814              // Init gradient
 815              $this->init($gradient, $r);
 816              
 817              for($i = 0; $i <= $r; $i += 0.45) {
 818              
 819                  $p = ceil((2 * M_PI * $i));
 820                  
 821                  if($p > 0) {
 822                      $interval = 360 / $p;
 823                  } else {
 824                      $interval = 360;
 825                  }
 826                  
 827                  $color = $this->color($i);
 828                  
 829                  for($j = 0; $j < 360; $j += $interval) {
 830                  
 831                      $rad = ($j / 360) * (2 * M_PI);
 832                      
 833                      $x = round($i * cos($rad));
 834                      $y = round($i * sin($rad));
 835                      
 836                      $l = sqrt($x * $x + $y * $y);
 837                      
 838                      if($l <= $r) {
 839                      
 840                          if(
 841                              array_key_exists((int)$x, $ok) === FALSE or
 842                              array_key_exists((int)$y, $ok[$x]) === FALSE
 843                          ) {
 844                          
 845                              // Print the point
 846                              $this->drawer->point($color, new awPoint($c->x + $x, $c->y + $y));
 847                              
 848                              $ok[(int)$x][(int)$y] = TRUE;
 849                          
 850                          }
 851                          
 852                      }
 853                  
 854                  }
 855                  
 856                  $color->free();
 857                  unset($color);
 858              
 859              }
 860          
 861          }
 862      
 863      }
 864      
 865  	protected function ellipseLinearGradient(awGradient $gradient, $x1, $y1, $x2, $y2) {
 866      
 867          // Gauche->droite : 90°
 868      
 869          if($y1 - $y2 > 0) {
 870      
 871              if($y1 - $y2 != $x2 - $x1) {
 872                  trigger_error("Linear gradients are only implemented on circle, not ellipses");
 873              }
 874              
 875              $r = ($x2 - $x1) / 2;
 876              
 877              // Init gradient
 878              $this->init($gradient, $x2 - $x1);
 879              
 880              for($i = -$r; $i <= $r; $i++) {
 881                  
 882                  $h = sin(acos($i / $r)) * $r;
 883                  
 884                  $color = $this->color($i + $r);
 885                  
 886                  if($gradient->angle === 90) {
 887                  
 888                      // Print the line
 889                      $p1 = new awPoint(
 890                          $x1 + $i + $r,
 891                          round(max($y2 + $r - $h + 1, $y2))
 892                      );
 893                      
 894                      $p2 = new awPoint(
 895                          $x1 + $i + $r,
 896                          round(min($y1 - $r + $h - 1, $y1))
 897                      );
 898                      
 899                  } else {
 900                  
 901                      // Print the line
 902                      $p1 = new awPoint(
 903                          round(max($x1 + $r - $h + 1, $x1)),
 904                          $y2 + $i + $r
 905                      );
 906                      
 907                      $p2 = new awPoint(
 908                          round(min($x2 - $r + $h - 1, $x2)),
 909                          $y2 + $i + $r
 910                      );
 911                      
 912                  }
 913                  
 914                  $this->drawer->filledRectangle($color, new awLine($p1, $p2));
 915                  
 916                  $color->free();
 917                  unset($color);
 918              
 919              }
 920          
 921          }
 922      
 923      }
 924      
 925  	protected function polygonLinearGradient(awLinearGradient $gradient, awPolygon $polygon) {
 926      
 927          $count = $polygon->count();
 928          
 929          if($count >= 3) {
 930          
 931              $left = $polygon->get(0);
 932              $right = $polygon->get($count - 1);
 933              
 934              if($gradient->angle === 0) {
 935              
 936                  // Get polygon maximum and minimum
 937                  $offset = $polygon->get(0);
 938                  $max = $min = $offset->y;
 939                  for($i = 1; $i < $count - 1; $i++) {
 940                      $offset = $polygon->get($i);
 941                      $max = max($max, $offset->y);
 942                      $min = min($min, $offset->y);
 943                  }
 944                  
 945                  $this->init($gradient, $max - $min);
 946              
 947                  $prev = $polygon->get(1);
 948                  
 949                  $sum = 0;
 950              
 951                  for($i = 2; $i < $count - 1; $i++) {
 952                  
 953                      $current = $polygon->get($i);
 954                      
 955                      $interval = 1;
 956                      
 957                      if($i !== $count - 2) {
 958                          $current->x -= $interval;
 959                      }
 960                      
 961                      if($current->x - $prev->x > 0) {
 962                  
 963                          // Draw rectangle
 964                          $x1 = $prev->x;
 965                          $x2 = $current->x;
 966                          $y1 = max($prev->y, $current->y);
 967                          $y2 = $left->y;
 968                          
 969                          $gradient = new awLinearGradient(
 970                              $this->color($max - $min - ($y2 - $y1)),
 971                              $this->color($max - $min),
 972                              0
 973                          );
 974                          
 975                          if($y1 > $y2) {
 976                              $y2 = $y1;
 977                          }
 978                          
 979                          $this->drawer->filledRectangle(
 980                              $gradient,
 981                              awLine::build($x1, $y1, $x2, $y2)
 982                          );
 983                          
 984                          $top = ($prev->y < $current->y) ? $current : $prev;
 985                          $bottom = ($prev->y >= $current->y) ? $current : $prev;
 986                          
 987                          $gradient = new awLinearGradient(
 988                              $this->color($bottom->y - $min),
 989                              $this->color($max - $min - ($y2 - $y1)),
 990                              0
 991                          );
 992                          
 993      
 994                          $gradientDrawer = new awGradientDrawer($this->drawer);
 995                          $gradientDrawer->drawFilledFlatTriangle(
 996                              $gradient,
 997                              new awPoint($prev->x, min($prev->y, $current->y)),
 998                              $top,
 999                              new awPoint($current->x, min($prev->y, $current->y))
1000                          );
1001                          unset($gradientDrawer);
1002                          
1003                          $sum += $current->x - $prev->x;
1004                          
1005                      }
1006                      
1007                      $prev = $current;
1008                      $prev->x += $interval;
1009                  
1010                  }
1011              
1012              } else if($gradient->angle === 90) {
1013                  
1014                  $width = $right->x - $left->x;
1015                  $this->init($gradient, $width);
1016                  
1017                  $pos = 1;
1018                  $next = $polygon->get($pos++);
1019                  
1020                  $this->next($polygon, $pos, $prev, $next);
1021              
1022                  for($i = 0; $i <= $width; $i++) {
1023                  
1024                      $x = $left->x + $i;
1025                      
1026                      $y1 = round($prev->y + ($next->y - $prev->y) * (($i + $left->x - $prev->x) / ($next->x - $prev->x)));
1027                      $y2 = $left->y;
1028                  
1029                      // Draw line
1030                      $color = $this->color($i);
1031                      // YaPB : PHP does not handle alpha on lines
1032                      $this->drawer->filledRectangle($color, awLine::build($x, $y1, $x, $y2));
1033                      $color->free();
1034                      unset($color);
1035                      
1036                      // Jump to next point
1037                      if($next->x == $i + $left->x) {
1038                      
1039                          $this->next($polygon, $pos, $prev, $next);
1040                          
1041                      }
1042                  
1043                  }
1044      
1045              }
1046              
1047          }
1048      
1049      }
1050      
1051  	private function next($polygon, &$pos, &$prev, &$next) {
1052      
1053          do {
1054              $prev = $next;
1055              $next = $polygon->get($pos++);
1056          }
1057          while($next->x - $prev->x == 0 and $pos < $polygon->count());
1058          
1059      }
1060      
1061      /**
1062       * Start colors
1063       *
1064       * @var int
1065       */
1066      private $r1, $g1, $b1, $a1;
1067      
1068      /**
1069       * Stop colors
1070       *
1071       * @var int
1072       */
1073      private $r2, $g2, $b2, $a2;
1074      
1075      /**
1076       * Gradient size in pixels
1077       *
1078       * @var int
1079       */
1080      private $size;
1081      
1082      
1083  	private function init(awGradient $gradient, $size) {
1084          
1085          list(
1086              $this->r1, $this->g1, $this->b1, $this->a1
1087          ) = $gradient->from->rgba();
1088          
1089          list(
1090              $this->r2, $this->g2, $this->b2, $this->a2
1091          ) = $gradient->to->rgba();
1092          
1093          $this->size = $size;
1094      }
1095      
1096  	private function color($pos) {
1097      
1098          return new awColor(
1099              $this->getRed($pos),
1100              $this->getGreen($pos),
1101              $this->getBlue($pos),
1102              $this->getAlpha($pos)
1103          );
1104          
1105      }
1106      
1107      
1108  	private function getRed($pos) {
1109          return (int)round($this->r1 + ($pos / $this->size) * ($this->r2 - $this->r1));
1110      }
1111      
1112  	private function getGreen($pos) {
1113          return (int)round($this->g1 + ($pos / $this->size) * ($this->g2 - $this->g1));
1114      }
1115      
1116  	private function getBlue($pos) {
1117          return (int)round($this->b1 + ($pos / $this->size) * ($this->b2 - $this->b1));
1118      }
1119      
1120  	private function getAlpha($pos) {
1121          return (int)round(($this->a1 + ($pos / $this->size) * ($this->a2 - $this->a1)) / 127 * 100);
1122      }
1123  
1124  }
1125  
1126  registerClass('GradientDrawer');
1127  ?>


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