Exemple #1
0
 protected function polygonLinearGradient(awLinearGradient $gradient, awPolygon $polygon)
 {
     $count = $polygon->count();
     if ($count >= 4) {
         $left = $polygon->get(0);
         $right = $polygon->get($count - 1);
         if ($gradient->angle === 0) {
             // Get polygon maximum and minimum
             $offset = $polygon->get(0);
             $max = $min = $offset->y;
             for ($i = 1; $i < $count - 1; $i++) {
                 $offset = $polygon->get($i);
                 $max = max($max, $offset->y);
                 $min = min($min, $offset->y);
             }
             $this->init($gradient, $max - $min);
             $prev = $polygon->get(1);
             $sum = 0;
             for ($i = 2; $i < $count - 1; $i++) {
                 $current = $polygon->get($i);
                 $interval = 1;
                 if ($i !== $count - 2) {
                     $current->x -= $interval;
                 }
                 if ($current->x - $prev->x > 0) {
                     // Draw rectangle
                     $x1 = $prev->x;
                     $x2 = $current->x;
                     $y1 = max($prev->y, $current->y);
                     $y2 = $left->y;
                     $gradient = new awLinearGradient($this->color($max - $min - ($y2 - $y1)), $this->color($max - $min), 0);
                     if ($y1 > $y2) {
                         $y2 = $y1;
                     }
                     $this->driver->filledRectangle($gradient, awLine::build($x1, $y1, $x2, $y2));
                     $top = $prev->y < $current->y ? $current : $prev;
                     $bottom = $prev->y >= $current->y ? $current : $prev;
                     $gradient = new awLinearGradient($this->color($bottom->y - $min), $this->color($max - $min - ($y2 - $y1)), 0);
                     $gradientDriver = new awGDGradientDriver($this->driver);
                     $gradientDriver->drawFilledFlatTriangle($gradient, new awPoint($prev->x, min($prev->y, $current->y)), $top, new awPoint($current->x, min($prev->y, $current->y)));
                     unset($gradientDriver);
                     $sum += $current->x - $prev->x;
                 }
                 $prev = $current;
                 $prev->x += $interval;
             }
         } elseif ($gradient->angle === 90) {
             $width = $right->x - $left->x;
             $this->init($gradient, $width);
             $pos = 1;
             $next = $polygon->get($pos++);
             $this->next($polygon, $pos, $prev, $next);
             for ($i = 0; $i <= $width; $i++) {
                 $x = $left->x + $i;
                 $y1 = round($prev->y + ($next->y - $prev->y) * (($i + $left->x - $prev->x) / ($next->x - $prev->x)));
                 $y2 = $left->y;
                 // Draw line
                 $color = $this->color($i);
                 // YaPB : PHP does not handle alpha on lines
                 $this->driver->filledRectangle($color, awLine::build($x, $y1, $x, $y2));
                 unset($color);
                 // Jump to next point
                 if ($next->x == $i + $left->x) {
                     $this->next($polygon, $pos, $prev, $next);
                 }
             }
         }
     } elseif ($count === 3) {
         $this->drawFilledTriangle($gradient, $polygon);
     }
 }
Exemple #2
0
 /**
  * Draw an awFileFont object on a GD ressource
  *
  * @param awGDDriver $driver The awGDDriver object containing the ressource to draw upon
  * @param awText $text The awText object containing the string to draw
  * @param awPoint $point Where to draw the string from
  * @param float $width The width of the area containing the text
  */
 private function gdString(awGDDriver $driver, awText $text, awPoint $point, $width = NULL)
 {
     // Make easier font positionment
     $text->setText($text->getText() . " ");
     $font = $text->getFont();
     if ($font instanceof awTTFFont === FALSE and $font->getExtension() === NULL) {
         $font->setExtension('ttf');
     }
     $box = imagettfbbox($font->size, $text->getAngle(), $font->name . '.' . $font->getExtension(), $text->getText());
     $textHeight = -$box[5];
     $box = imagettfbbox($font->size, 90, $font->name . '.' . $font->getExtension(), $text->getText());
     $textWidth = abs($box[6] - $box[2]);
     // Restore old text
     $text->setText(substr($text->getText(), 0, strlen($text->getText()) - 1));
     $textString = $text->getText();
     // Split text if needed
     if ($width !== NULL) {
         $characters = floor($width / $this->getGDAverageWidth($font));
         $textString = wordwrap($textString, $characters, "\n", TRUE);
     }
     $color = $text->getColor();
     $rgb = $driver->getColor($color);
     imagettftext($driver->resource, $font->size, $text->getAngle(), $driver->x + $point->x + $textWidth * sin($text->getAngle() / 180 * M_PI), $driver->y + $point->y + $textHeight, $rgb, $font->name . '.' . $font->getExtension(), $textString);
 }