[ Index ] |
|
Code source de Cr@wltr@ck 2.2.1 |
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 require_once dirname(__FILE__)."/../Driver.class.php"; 11 12 /** 13 * Draw your objects 14 * 15 * @package Artichow 16 */ 17 class awMingDriver extends awDriver { 18 19 /** 20 * The Flash movie 21 * 22 * @var $movie 23 */ 24 public $movie; 25 26 public function __construct() { 27 28 parent::__construct(); 29 30 $this->driverString = 'ming'; 31 32 // Nice defaults 33 ming_setScale(20.0); 34 ming_useswfversion(6); 35 36 } 37 38 /** 39 * Initialize the driver for a particular awImage object 40 * 41 * @param awImage $image 42 */ 43 public function init(awImage $image) { 44 45 if($this->movie === NULL) { 46 $this->setImageSize($image->width, $image->height); 47 48 // Create movie 49 $this->movie = new SWFMovie(); 50 if(!$this->movie) { 51 awImage::drawError("Class Image: Unable to create a graph."); 52 } 53 54 $this->movie->setDimension($image->width, $image->height); 55 56 $this->setAntiAliasing($image->getAntiAliasing()); 57 58 // Original color 59 $this->filledRectangle( 60 new awWhite, 61 new awLine( 62 new awPoint(0, 0), 63 new awPoint($this->imageWidth, $this->imageHeight) 64 ) 65 ); 66 67 $shadow = $image->shadow; 68 if($shadow !== NULL) { 69 $shadow = $shadow->getSpace(); 70 $p1 = new awPoint($shadow->left, $shadow->top); 71 $p2 = new awPoint($this->imageWidth - $shadow->right - 1, $this->imageHeight - $shadow->bottom - 1); 72 73 // Draw image background 74 $this->filledRectangle($image->getBackground(), new awLine($p1, $p2)); 75 76 // Draw image border 77 $image->border->rectangle($this, $p1, $p2); 78 } 79 } 80 } 81 82 /** 83 * Initialize the Driver for a particular FileImage object 84 * 85 * @param awFileImage $fileImage The FileImage object to work on 86 * @param string $file Image filename 87 */ 88 public function initFromFile(awFileImage $fileImage, $file) { 89 90 } 91 92 /** 93 * Change the image size 94 * 95 * @param int $width Image width 96 * @param int $height Image height 97 */ 98 public function setImageSize($width, $height) { 99 $this->imageWidth = $width; 100 $this->imageHeight = $height; 101 } 102 103 /** 104 * Inform the driver of the position of your image 105 * 106 * @param float $x Position on X axis of the center of the component 107 * @param float $y Position on Y axis of the center of the component 108 */ 109 public function setPosition($x, $y) { 110 // Calculate absolute position 111 $this->x = round($x * $this->imageWidth - $this->w / 2); 112 $this->y = round($y * $this->imageHeight - $this->h / 2); 113 } 114 115 /** 116 * Inform the driver of the position of your image 117 * This method need absolutes values 118 * 119 * @param int $x Left-top corner X position 120 * @param int $y Left-top corner Y position 121 */ 122 public function setAbsPosition($x, $y) { 123 $this->x = $x; 124 $this->y = $y; 125 } 126 127 /** 128 * Move the position of the image 129 * 130 * @param int $x Add this value to X axis 131 * @param int $y Add this value to Y axis 132 */ 133 public function movePosition($x, $y) { 134 $this->x += (int)$x; 135 $this->y += (int)$y; 136 } 137 138 /** 139 * Inform the driver of the size of your image 140 * Height and width must be between 0 and 1. 141 * 142 * @param int $w Image width 143 * @param int $h Image height 144 * @return array Absolute width and height of the image 145 */ 146 public function setSize($w, $h) { 147 148 // Calcul absolute size 149 $this->w = round($w * $this->imageWidth); 150 $this->h = round($h * $this->imageHeight); 151 152 return $this->getSize(); 153 154 } 155 156 /** 157 * Inform the driver of the size of your image 158 * You can set absolute size with this method. 159 * 160 * @param int $w Image width 161 * @param int $h Image height 162 */ 163 public function setAbsSize($w, $h) { 164 $this->w = $w; 165 $this->h = $h; 166 167 return $this->getSize(); 168 } 169 170 /** 171 * Get the size of the component handled by the driver 172 * 173 * @return array Absolute width and height of the component 174 */ 175 public function getSize() { 176 return array($this->w, $this->h); 177 } 178 179 /** 180 * Turn antialiasing on or off 181 * 182 * @var bool $bool 183 */ 184 public function setAntiAliasing($bool) { 185 if($this->movie !== NULL) { 186 187 $actionscript = ' 188 _quality = "%s"; 189 '; 190 191 if((bool)$bool) { 192 $actionscript = sprintf($actionscript, 'high'); 193 } else { 194 $actionscript = sprintf($actionscript, 'low'); 195 } 196 197 $this->movie->add(new SWFAction(str_replace("\r", "", $actionscript))); 198 } 199 } 200 201 /** 202 * When passed a Color object, returns the corresponding 203 * color identifier (driver dependant). 204 * 205 * @param awColor $color A Color object 206 * @return array $rgba A color identifier representing the color composed of the given RGB components 207 */ 208 public function getColor(awColor $color) { 209 210 // Ming simply works with R, G, B and Alpha values. 211 list($red, $green, $blue, $alpha) = $color->rgba(); 212 213 // However, the Ming alpha channel ranges from 255 (opaque) to 0 (transparent), 214 // while the awColor alpha channel ranges from 0 (opaque) to 100 (transparent). 215 // First, we convert from 0-100 to 0-255. 216 $alpha = (int)($alpha * 255 / 100); 217 218 // Then from 0-255 to 255-0. 219 $alpha = abs($alpha - 255); 220 221 return array($red, $green, $blue, $alpha); 222 } 223 224 /** 225 * Draw an image here 226 * 227 * @param awImage $image Image 228 * @param int $p1 Image top-left point 229 * @param int $p2 Image bottom-right point 230 */ 231 public function copyImage(awImage $image, awPoint $p1, awPoint $p2) { 232 233 } 234 235 /** 236 * Draw an image here 237 * 238 * @param awImage $image Image 239 * @param int $d1 Destination top-left position 240 * @param int $d2 Destination bottom-right position 241 * @param int $s1 Source top-left position 242 * @param int $s2 Source bottom-right position 243 * @param bool $resample Resample image ? (default to TRUE) 244 */ 245 public function copyResizeImage(awImage $image, awPoint $d1, awPoint $d2, awPoint $s1, awPoint $s2, $resample = TRUE) { 246 247 } 248 249 /** 250 * Draw a string 251 * 252 * @var awText $text Text to print 253 * @param awPoint $point Draw the text at this point 254 * @param int $width Text max width 255 */ 256 public function string(awText $text, awPoint $point, $width = NULL) { 257 $font = $text->getFont(); 258 259 // Can we deal with that font? 260 if($this->isCompatibleWithFont($font) === FALSE) { 261 awImage::drawError('Class MingDriver: Incompatible font type (\''.get_class($font).'\')'); 262 } 263 264 // Ming can only work with awFileFont objects for now 265 // (i.e. awFDBFont, or awTuffy et al.) 266 $fontDriver = $this->fileFontDriver; 267 268 if($text->getBackground() !== NULL or $text->border->visible()) { 269 270 list($left, $right, $top, $bottom) = $text->getPadding(); 271 272 $textWidth = $fontDriver->getTextWidth($text, $this); 273 $textHeight = $fontDriver->getTextHeight($text, $this); 274 275 $x1 = floor($point->x - $left); 276 $y1 = floor($point->y - $top); 277 $x2 = $x1 + $textWidth + $left + $right; 278 $y2 = $y1 + $textHeight + $top + $bottom; 279 280 $this->filledRectangle( 281 $text->getBackground(), 282 awLine::build($x1, $y1, $x2, $y2) 283 ); 284 285 $text->border->rectangle( 286 $this, 287 new awPoint($x1 - 1, $y1 - 1), 288 new awPoint($x2 + 1, $y2 + 1) 289 ); 290 291 } 292 293 $fontDriver->string($this, $text, $point, $width); 294 } 295 296 /** 297 * Draw a pixel 298 * 299 * @param awColor $color Pixel color 300 * @param awPoint $p 301 */ 302 public function point(awColor $color, awPoint $p) { 303 if($p->isHidden() === FALSE) { 304 list($red, $green, $blue, $alpha) = $this->getColor($color); 305 306 $point = new SWFShape(); 307 $point->setLine(1, $red, $green, $blue, $alpha); 308 $point->movePenTo($this->x + round($p->x), $this->y + round($p->y)); 309 $point->drawLine(0.5, 0.5); 310 $point->movePen(-0.5, 0); 311 $point->drawLine(0.5, -0.5); 312 313 $this->movie->add($point); 314 } 315 } 316 317 /** 318 * Draw a colored line 319 * 320 * @param awColor $color Line color 321 * @param awLine $line 322 * @param int $thickness Line tickness 323 */ 324 public function line(awColor $color, awLine $line) { 325 if($line->getThickness() > 0 and $line->isHidden() === FALSE) { 326 327 list($red, $green, $blue, $alpha) = $this->getColor($color); 328 329 $mingLine = new SWFShape(); 330 $mingLine->setLine($line->getThickness(), $red, $green, $blue, $alpha); 331 332 list($p1, $p2) = $line->getLocation(); 333 334 $mingLine->movePenTo($this->x + round($p1->x), $this->y + round($p1->y)); 335 336 switch($line->getStyle()) { 337 338 case awLine::SOLID : 339 $mingLine->drawLineTo($this->x + round($p2->x), $this->y + round($p2->y)); 340 $this->movie->add($mingLine); 341 break; 342 343 case awLine::DOTTED : 344 $size = sqrt(pow($p2->y - $p1->y, 2) + pow($p2->x - $p1->x, 2)); 345 $cos = ($p2->x - $p1->x) / $size; 346 $sin = ($p2->y - $p1->y) / $size; 347 348 for($i = 0; $i <= $size; $i += 2) { 349 $p = new awPoint( 350 round($i * $cos + $p1->x), 351 round($i * $sin + $p1->y) 352 ); 353 $this->point($color, $p); 354 } 355 356 break; 357 358 case awLine::DASHED : 359 $width = $p2->x - $p1->x; 360 $height = $p2->y - $p1->y; 361 $size = sqrt(pow($height, 2) + pow($width, 2)); 362 363 if($size == 0) { 364 return; 365 } 366 367 $cos = $width / $size; 368 $sin = $height / $size; 369 370 $functionX = ($width > 0) ? 'min' : 'max'; 371 $functionY = ($height > 0) ? 'min' : 'max'; 372 373 for($i = 0; $i <= $size; $i += 6) { 374 375 $t1 = new awPoint( 376 round($i * $cos + $p1->x), 377 round($i * $sin + $p1->y) 378 ); 379 380 $t2 = new awPoint( 381 round($functionX(($i + 3) * $cos, $width) + $p1->x), 382 round($functionY(($i + 3) * $sin, $height) + $p1->y) 383 ); 384 385 $this->line($color, new awLine($t1, $t2)); 386 387 } 388 389 break; 390 391 } 392 393 } 394 395 } 396 397 /** 398 * Draw a color arc 399 400 * @param awColor $color Arc color 401 * @param awPoint $center Point center 402 * @param int $width Ellipse width 403 * @param int $height Ellipse height 404 * @param int $from Start angle 405 * @param int $to End angle 406 */ 407 public function arc(awColor $color, awPoint $center, $width, $height, $from, $to) { 408 409 } 410 411 /** 412 * Draw an arc with a background color 413 * 414 * @param awColor $color Arc background color 415 * @param awPoint $center Point center 416 * @param int $width Ellipse width 417 * @param int $height Ellipse height 418 * @param int $from Start angle 419 * @param int $to End angle 420 */ 421 public function filledArc(awColor $color, awPoint $center, $width, $height, $from, $to) { 422 423 } 424 425 /** 426 * Draw a colored ellipse 427 * 428 * @param awColor $color Ellipse color 429 * @param awPoint $center Ellipse center 430 * @param int $width Ellipse width 431 * @param int $height Ellipse height 432 */ 433 public function ellipse(awColor $color, awPoint $center, $width, $height) { 434 435 } 436 437 /** 438 * Draw an ellipse with a background 439 * 440 * @param mixed $background Background (can be a color or a gradient) 441 * @param awPoint $center Ellipse center 442 * @param int $width Ellipse width 443 * @param int $height Ellipse height 444 */ 445 public function filledEllipse($background, awPoint $center, $width, $height) { 446 447 } 448 449 /** 450 * Draw a colored rectangle 451 * 452 * @param awColor $color Rectangle color 453 * @param awLine $line Rectangle diagonale 454 * @param awPoint $p2 455 */ 456 public function rectangle(awColor $color, awLine $line) { 457 list($p1, $p2) = $line->getLocation(); 458 459 // Get Red, Green, Blue and Alpha values for the line 460 list($r, $g, $b, $a) = $this->getColor($color); 461 462 // Calculate the coordinates of the two other points of the rectangle 463 $p3 = new Point($p1->x, $p2->y); 464 $p4 = new Point($p2->x, $p1->y); 465 466 /* <php5> */ 467 $side = clone $line; 468 /* </php5> */ 469 470 /* <php4> -- 471 $side = new Line($p1, $p2); 472 -- </php4> */ 473 474 // Draw the four sides of the rectangle, clockwise 475 if( 476 ($p1->x <= $p2->x and $p1->y <= $p2->y) 477 or 478 ($p1->x >= $p2->x and $p1->y >= $p2->y) 479 ) { 480 $side->setLocation($p1, $p4); 481 $this->line($color, $side); 482 483 $side->setLocation($p4, $p2); 484 $this->line($color, $side); 485 486 $side->setLocation($p2, $p3); 487 $this->line($color, $side); 488 489 $side->setLocation($p3, $p1); 490 $this->line($color, $side); 491 } else { 492 $side->setLocation($p1, $p3); 493 $this->line($color, $side); 494 495 $side->setLocation($p3, $p2); 496 $this->line($color, $side); 497 498 $side->setLocation($p2, $p4); 499 $this->line($color, $side); 500 501 $side->setLocation($p4, $p1); 502 $this->line($color, $side); 503 } 504 } 505 506 /** 507 * Draw a rectangle with a background 508 * 509 * @param mixed $background Background (can be a color or a gradient) 510 * @param awLine $line Rectangle diagonale 511 */ 512 public function filledRectangle($background, awLine $line) { 513 list($p1, $p2) = $line->getLocation(); 514 515 // Common shape settings 516 $shape = new SWFShape(); 517 $shape->setLine(0); 518 519 if($background instanceof awColor) { 520 521 // Get the Red, Green, Blue and Alpha values 522 list($r, $g, $b, $a) = $this->getColor($background); 523 $shape->setRightFill($r, $g, $b, $a); 524 525 } else if($background instanceof awGradient) { 526 527 // Get the Gradient object as an SWFGradient one 528 list($flashGradient, $style) = $this->getGradient($background); 529 530 $fill = $shape->addFill($flashGradient, $style); 531 532 // Angles between Artichow and Ming don't match. 533 // Don't use abs() or vertical gradients get inverted. 534 $angle = $background->angle - 90; 535 $fill->rotateTo($angle); 536 537 // Move the gradient based on the position of the rectangle we're drawing 538 $centerX = min($p1->x, $p2->y) + abs($p1->x - $p2->x) / 2; 539 $centerY = min($p1->y, $p2->y) + abs($p1->y - $p2->y) / 2; 540 $fill->moveTo($centerX, $centerY); 541 542 // Ming draws its gradients on a 1600x1600 image, 543 // so we have to resize it. 544 if($angle === -90) { 545 $ratio = abs($p1->y - $p2->y) / 1600; 546 } else { 547 $ratio = abs($p1->x - $p2->x) / 1600; 548 } 549 $fill->scaleTo($ratio); 550 551 $shape->setRightFill($fill); 552 553 } 554 555 // Set starting position 556 $shape->movePenTo($this->x + round($p1->x), $this->y + round($p1->y)); 557 558 // Depending on the points' relative positions, 559 // we have two drawing possibilities 560 if( 561 ($p1->x <= $p2->x and $p1->y <= $p2->y) 562 or 563 ($p1->x >= $p2->x and $p1->y >= $p2->y) 564 ) { 565 $shape->drawLineTo($this->x + round($p2->x), $this->y + round($p1->y)); 566 $shape->drawLineTo($this->x + round($p2->x), $this->y + round($p2->y)); 567 $shape->drawLineTo($this->x + round($p1->x), $this->y + round($p2->y)); 568 $shape->drawLineTo($this->x + round($p1->x), $this->y + round($p1->y)); 569 } else { 570 $shape->drawLineTo($this->x + round($p1->x), $this->y + round($p2->y)); 571 $shape->drawLineTo($this->x + round($p2->x), $this->y + round($p2->y)); 572 $shape->drawLineTo($this->x + round($p2->x), $this->y + round($p1->y)); 573 $shape->drawLineTo($this->x + round($p1->x), $this->y + round($p1->y)); 574 } 575 576 $this->movie->add($shape); 577 } 578 579 /** 580 * Draw a polygon 581 * 582 * @param awColor $color Polygon color 583 * @param Polygon A polygon 584 */ 585 public function polygon(awColor $color, awPolygon $polygon) { 586 $points = $polygon->all(); 587 $count = count($points); 588 589 if($count > 1) { 590 591 $side = new awLine; 592 $side->setStyle($polygon->getStyle()); 593 $side->setThickness($polygon->getThickness()); 594 595 $prev = $points[0]; 596 597 for($i = 1; $i < $count; $i++) { 598 $current = $points[$i]; 599 $side->setLocation($prev, $current); 600 $this->line($color, $side); 601 $prev = $current; 602 } 603 604 // Close the polygon 605 $side->setLocation($prev, $points[0]); 606 $this->line($color, $side); 607 } 608 } 609 610 /** 611 * Draw a polygon with a background 612 * 613 * @param mixed $background Background (can be a color or a gradient) 614 * @param Polygon A polygon 615 */ 616 public function filledPolygon($background, awPolygon $polygon) { 617 $shape = new SWFShape(); 618 619 if($background instanceof awColor) { 620 list($red, $green, $blue, $alpha) = $this->getColor($background); 621 622 $shape->setRightFill($red, $green, $blue, $alpha); 623 } elseif($background instanceof awGradient) { 624 list($flashGradient, $style) = $this->getGradient($background); 625 626 $fill = $shape->addFill($flashGradient, $style); 627 628 list($xMin, $xMax) = $polygon->getBoxXRange(); 629 list($yMin, $yMax) = $polygon->getBoxYRange(); 630 631 if($background->angle === 0) { 632 $fill->scaleTo(($yMax - $yMin) / 1600); 633 } else { 634 $fill->scaleTo(($xMax - $xMin) / 1600); 635 } 636 $fill->moveTo($xMin + ($xMax - $xMin) / 2, $yMin + ($yMax - $yMin) / 2); 637 638 $shape->setRightFill($fill); 639 } 640 641 $points = $polygon->all(); 642 $count = count($points); 643 644 if($count > 1) { 645 646 $prev = $points[0]; 647 648 $shape->movePenTo($prev->x, $prev->y); 649 650 for($i = 1; $i < $count; $i++) { 651 $current = $points[$i]; 652 $shape->drawLineTo($current->x, $current->y); 653 } 654 655 // Close the polygon 656 $shape->drawLineTo($prev->x, $prev->y); 657 658 $this->movie->add($shape); 659 660 } 661 } 662 663 /** 664 * Sends the image, as well as the correct HTTP headers, to the browser 665 * 666 * @param awImage $image The Image object to send 667 */ 668 public function send(awImage $image) { 669 $this->drawImage($image); 670 } 671 672 /** 673 * Get the image as binary data 674 * 675 * @param awImage $image 676 */ 677 public function get(awImage $image) { 678 return $this->drawImage($image, TRUE, FALSE); 679 } 680 681 public function getTextWidth(awText $text) { 682 $font = $text->getFont(); 683 if($this->isCompatibleWithFont($font) === FALSE) { 684 awImage::drawError('Class MingDriver: Incompatible font type (\''.get_class($font).'\')'); 685 } 686 687 // Ming only supports FileFont 688 $fontDriver = $this->fileFontDriver; 689 690 return $fontDriver->getTextWidth($text, $this); 691 } 692 693 public function getTextHeight(awText $text) { 694 $font = $text->getFont(); 695 if($this->isCompatibleWithFont($font) === FALSE) { 696 awImage::drawError('Class MingDriver: Incompatible font type (\''.get_class($font).'\')'); 697 } 698 699 // Ming only supports FileFont 700 $fontDriver = $this->fileFontDriver; 701 702 return $fontDriver->getTextHeight($text, $this); 703 } 704 705 protected function isCompatibleWithFont(awFont $font) { 706 if($font instanceof awTTFFont or $font instanceof awPHPFont) { 707 return FALSE; 708 } else { 709 return TRUE; 710 } 711 } 712 713 private function drawImage(awImage $image, $return = FALSE, $header = TRUE) { 714 715 // Send headers to the browser 716 if($header === TRUE) { 717 $image->sendHeaders(); 718 } 719 720 if($return) { 721 ob_start(); 722 } 723 724 $this->movie->output(); 725 726 if($return) { 727 return ob_get_clean(); 728 } 729 } 730 731 /** 732 * Convert an awGradient object to an SWFGradient one. 733 * Returns an object as well as the style of the Flash gradient. 734 * 735 * @param awGradient $gradient The awGradient object to convert 736 * @return array 737 */ 738 private function getGradient(awGradient $gradient) { 739 $flashGradient = new SWFGradient(); 740 741 // Get RGBA values for the gradient boundaries 742 list($r1, $g1, $b1, $a1) = $this->getColor($gradient->from); 743 list($r2, $g2, $b2, $a2) = $this->getColor($gradient->to); 744 745 $flashGradient->addEntry(0, $r1, $g1, $b1, $a1); 746 747 if($gradient instanceof awBilinearGradient) { 748 749 $flashGradient->addEntry($gradient->center, $r2, $g2, $b2, $a2); 750 $flashGradient->addEntry(1, $r1, $g1, $b1, $a1); 751 752 return array($flashGradient, SWFFILL_LINEAR_GRADIENT); 753 } else { 754 755 $flashGradient->addEntry(1, $r2, $g2, $b2, $a2); 756 757 if($gradient instanceof awLinearGradient) { 758 return array($flashGradient, SWFFILL_LINEAR_GRADIENT); 759 } else { 760 return array($flashGradient, SWFFILL_RADIAL_GRADIENT); 761 } 762 } 763 } 764 // abstract private function getPolygonPoints(awPolygon $polygon); 765 766 } 767 768 registerClass('MingDriver'); 769 770 /* 771 * Check for ming presence 772 */ 773 if(function_exists('ming_useswfversion') === FALSE) { 774 awImage::drawErrorFile('missing-ming'); 775 } 776 777 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Sep 6 14:14:11 2007 | par Balluche grâce à PHPXref 0.7 |