source: sandbox/filemanager/tp/dompdf/include/abstract_renderer.cls.php @ 1575

Revision 1575, 19.7 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

Line 
1<?php
2/**
3 * DOMPDF - PHP5 HTML to PDF renderer
4 *
5 * File: $RCSfile: abstract_renderer.cls.php,v $
6 * Created on: 2004-06-01
7 *
8 * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library in the file LICENSE.LGPL; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA
24 *
25 * Alternatively, you may distribute this software under the terms of the
26 * PHP License, version 3.0 or later.  A copy of this license should have
27 * been distributed with this file in the file LICENSE.PHP .  If this is not
28 * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
29 *
30 * The latest version of DOMPDF might be available at:
31 * http://www.digitaljunkies.ca/dompdf
32 *
33 * @link http://www.digitaljunkies.ca/dompdf
34 * @copyright 2004 Benj Carson
35 * @author Benj Carson <benjcarson@digitaljunkies.ca>
36 * @package dompdf
37 * @version 0.5.1
38 */
39
40/* $Id: abstract_renderer.cls.php,v 1.6 2006/07/07 21:31:02 benjcarson Exp $ */
41
42/**
43 * Base renderer class
44 *
45 * @access private
46 * @package dompdf
47 */
48abstract class Abstract_Renderer {
49
50  /**
51   * Rendering backend
52   *
53   * @var Canvas
54   */
55  protected $_canvas;
56
57  /**
58   * Current dompdf instance
59   *
60   * @var DOMPDF
61   */
62  protected $_dompdf;
63 
64  /**
65   * Class constructor
66   *
67   * @param DOMPDF $dompdf The current dompdf instance
68   */
69  function __construct(DOMPDF $dompdf) {
70    $this->_dompdf = $dompdf;
71    $this->_canvas = $dompdf->get_canvas();
72  }
73 
74  /**
75   * Render a frame.
76   *
77   * Specialized in child classes
78   *
79   * @param Frame $frame The frame to render
80   */
81  abstract function render(Frame $frame);
82
83  //........................................................................
84
85  /**
86   * Render a background image over a rectangular area
87   *
88   * @param string $img      The background image to load
89   * @param float  $x        The left edge of the rectangular area
90   * @param float  $y        The top edge of the rectangular area
91   * @param float  $width    The width of the rectangular area
92   * @param float  $height   The height of the rectangular area
93   * @param Style  $style    The associated Style object
94   */
95  protected function _background_image($url, $x, $y, $width, $height, $style) {
96    $sheet = $style->get_stylesheet();
97
98    // Skip degenerate cases
99    if ( $width == 0 || $height == 0 )
100      return;
101   
102    list($img, $ext) = Image_Cache::resolve_url($url,
103                                                $sheet->get_protocol(),
104                                                $sheet->get_host(),
105                                                $sheet->get_base_path());
106
107   
108    list($bg_x, $bg_y) = $style->background_position;
109    $repeat = $style->background_repeat;
110
111    if ( !is_percent($bg_x) )
112      $bg_x = $style->length_in_pt($bg_x);
113    if ( !is_percent($bg_y) )
114      $bg_y = $style->length_in_pt($bg_y);
115
116    $repeat = $style->background_repeat;
117    $position = $style->background_position;
118    $bg_color = $style->background_color;
119   
120    // Bail if the image is no good
121    if ( $img == DOMPDF_LIB_DIR . "/res/broken_image.png" )
122      return;
123   
124    $ext = strtolower($ext);
125   
126    list($img_w, $img_h) = getimagesize($img);
127
128    $bg_width = round($width * DOMPDF_DPI / 72);
129    $bg_height = round($height * DOMPDF_DPI / 72);
130
131    // Create a new image to fit over the background rectangle
132    $bg = imagecreatetruecolor($bg_width, $bg_height);
133    if ( $bg_color == "transparent" )
134      $bg_color = array(1,1,1);
135   
136    list($r,$g,$b) = $bg_color;
137    $r *= 255; $g *= 255; $b *= 255;
138
139    // Clip values
140    $r = $r > 255 ? 255 : $r;
141    $g = $g > 255 ? 255 : $g;
142    $b = $b > 255 ? 255 : $b;
143     
144    $r = $r < 0 ? 0 : $r;
145    $g = $g < 0 ? 0 : $g;
146    $b = $b < 0 ? 0 : $b;
147
148    $clear = imagecolorallocate($bg,round($r),round($g),round($b));
149    imagecolortransparent($bg, $clear);
150    imagefill($bg,1,1,$clear);
151   
152    switch ($ext) {
153
154    case "png":
155      $src = imagecreatefrompng($img);
156      break;
157     
158    case "jpg":
159    case "jpeg":
160      $src = imagecreatefromjpeg($img);
161      break;
162     
163    case "gif":
164      $src = imagecreatefromgif($img);
165      break;
166
167    default:
168      return; // Unsupported image type
169    }
170
171    if ( is_percent($bg_x) ) {
172      // The point $bg_x % from the left edge of the image is placed
173      // $bg_x % from the left edge of the background rectangle
174      $p = ((float)$bg_x)/100.0;
175      $x1 = $p * $img_w;
176      $x2 = $p * $bg_width;
177
178      $bg_x = $x2 - $x1;
179    }
180     
181    if ( is_percent($bg_y) ) {
182      // The point $bg_y % from the left edge of the image is placed
183      // $bg_y % from the left edge of the background rectangle
184      $p = ((float)$bg_y)/100.0;
185      $y1 = $p * $img_h;
186      $y2 = $p * $bg_height;
187
188      $bg_y = $y2 - $y1;
189    }
190
191    // Copy regions from the source image to the background
192    if ( $repeat == "no-repeat" ||
193         ($repeat == "repeat-x" && $img_w >= $bg_width) ||
194         ($repeat == "repeat-y" && $img_h >= $bg_height) ||
195         ($repeat == "repeat" && $img_w >= $bg_width && $img_h >= $bg_height) ) {
196     
197      // Simply place the image on the background
198      $src_x = 0;
199      $src_y = 0;
200      $dst_x = $bg_x;
201      $dst_y = $bg_y;
202     
203      if ( $bg_x < 0 ) {
204        $dst_x = 0;
205        $src_x = -$bg_x;
206      }
207
208      if ( $bg_y < 0 ) {
209        $dst_y = 0;
210        $src_y = -$bg_y;
211      }
212
213      $bg_x = round($bg_x * DOMPDF_DPI / 72);
214      $bg_y = round($bg_y * DOMPDF_DPI / 72);
215
216      imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);     
217
218    } else if ( $repeat == "repeat-x" ) {
219      $src_x = 0;
220      $src_y = 0;
221
222      $dst_y = $bg_y;
223
224      if ( $bg_y < 0 ) {
225        $dst_y = 0;
226        $src_y = -$bg_y;
227      }
228
229      if ( $bg_x < 0 )
230        $start_x = $bg_x;
231      else
232        $start_x = $bg_x % $img_w - $img_w;
233
234      for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
235        if ( $bg_x < 0 ) {
236          $dst_x = 0;
237          $src_x = -$bg_x;
238          $w = $img_w + $bg_x;
239        } else {         
240          $dst_x = $bg_x;
241          $src_x = 0;
242          $w = $img_w;
243        }
244        imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);     
245      }
246     
247    } else if ( $repeat == "repeat-y" ) {
248      $src_x = 0;
249      $src_y = 0;
250
251      $dst_x = $bg_x;
252
253      if ( $bg_x < 0 ) {
254        $dst_x = 0;
255        $src_x = -$bg_x;
256      }
257
258      if ( $bg_y < 0 )
259        $start_y = $bg_y;
260      else
261        $start_y = $bg_y % $img_h - $img_h;
262
263      for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
264        if ( $bg_y < 0 ) {
265          $dst_y = 0;
266          $src_y = -$bg_y;
267          $h = $img_h + $bg_y;
268        } else {         
269          $dst_y = $bg_y;
270          $src_y = 0;
271          $h = $img_h;
272        }
273        imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
274      }
275     
276    } else {
277
278      if ( $bg_x < 0 )
279        $start_x = $bg_x;
280      else
281        $start_x = $bg_x % $img_w - $img_w;
282
283      if ( $bg_y < 0 )
284        $start_y = $bg_y;
285      else
286        $start_y = $bg_y % $img_h - $img_h;
287     
288      for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
289        for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
290
291          if ( $bg_x < 0 ) {
292            $dst_x = 0;
293            $src_x = -$bg_x;
294            $w = $img_w + $bg_x;
295          } else {         
296            $dst_x = $bg_x;
297            $src_x = 0;
298            $w = $img_w;
299          }
300         
301          if ( $bg_y < 0 ) {
302            $dst_y = 0;
303            $src_y = -$bg_y;
304            $h = $img_h + $bg_y;
305          } else {         
306            $dst_y = $bg_y;
307            $src_y = 0;
308            $h = $img_h;
309          }
310          imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
311        }
312      }
313    }
314
315
316   
317    $tmp_file = tempnam(DOMPDF_TEMP_DIR, "dompdf_img_");
318    imagepng($bg, $tmp_file);
319    $this->_canvas->image($tmp_file, "png", $x, $y, $width, $height);
320    unlink($tmp_file);
321  }
322 
323  protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
324    return;
325  }
326 
327  // Border rendering functions
328  protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
329    list($top, $right, $bottom, $left) = $widths;
330
331    if ( $$side < 2 )
332      $dash = array($$side, 2);
333    else
334      $dash = array($$side);
335 
336   
337    switch ($side) {
338
339    case "top":
340      $delta = $top / 2;
341    case "bottom":
342      $delta = isset($delta) ? $delta : -$bottom / 2;
343      $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, $dash);
344      break;
345
346    case "left":
347      $delta = $left / 2;
348    case "right":
349      $delta = isset($delta) ? $delta : - $right / 2;
350      $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, $dash);
351      break;
352
353    default:
354      return;
355
356    }
357  }
358
359 
360  protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
361    list($top, $right, $bottom, $left) = $widths;
362
363    switch ($side) {
364
365    case "top":
366      $delta = $top / 2;
367    case "bottom":
368      $delta = isset($delta) ? $delta : -$bottom / 2;
369      $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, array(3 * $$side));
370      break;
371
372    case "left":
373      $delta = $left / 2;
374    case "right":
375      $delta = isset($delta) ? $delta : - $right / 2;
376      $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, array(3 * $$side));
377      break;
378
379    default:
380      return;
381    }
382   
383  }
384
385 
386  protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
387    list($top, $right, $bottom, $left) = $widths;
388
389    // All this polygon business is for beveled corners...
390    switch ($side) {
391
392    case "top":
393      if ( $corner_style == "bevel" ) {
394       
395        $points = array($x, $y,
396                        $x + $length, $y,
397                        $x + $length - $right, $y + $top,
398                        $x + $left, $y + $top);
399        $this->_canvas->polygon($points, $color, null, null, true);
400      } else
401        $this->_canvas->filled_rectangle($x, $y, $length, $top, $color);
402     
403      break;
404     
405    case "bottom":
406      if ( $corner_style == "bevel" ) {
407        $points = array($x, $y,
408                        $x + $length, $y,
409                        $x + $length - $right, $y - $bottom,
410                        $x + $left, $y - $bottom);
411        $this->_canvas->polygon($points, $color, null, null, true);
412      } else
413        $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $bottom, $color);
414     
415      break;
416     
417    case "left":
418      if ( $corner_style == "bevel" ) {
419        $points = array($x, $y,
420                        $x, $y + $length,
421                        $x + $left, $y + $length - $bottom,
422                        $x + $left, $y + $top);
423        $this->_canvas->polygon($points, $color, null, null, true);
424      } else
425        $this->_canvas->filled_rectangle($x, $y, $left, $length, $color);
426     
427      break;
428     
429    case "right":
430      if ( $corner_style == "bevel" ) {
431        $points = array($x, $y,
432                        $x, $y + $length,
433                        $x - $right, $y + $length - $bottom,
434                        $x - $right, $y + $top);
435        $this->_canvas->polygon($points, $color, null, null, true);
436      } else
437        $this->_canvas->filled_rectangle($x - $right, $y, $right, $length, $color);
438
439      break;
440
441    default:
442      return;
443
444    }
445       
446  }
447
448
449  protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
450    list($top, $right, $bottom, $left) = $widths;
451   
452    $line_width = $$side / 4;
453   
454    // We draw the outermost edge first. Points are ordered: outer left,
455    // outer right, inner right, inner left, or outer top, outer bottom,
456    // inner bottom, inner top.
457    switch ($side) {
458
459    case "top":
460      if ( $corner_style == "bevel" ) {
461        $left_line_width = $left / 4;
462        $right_line_width = $right / 4;
463       
464        $points = array($x, $y,
465                        $x + $length, $y,
466                        $x + $length - $right_line_width, $y + $line_width,
467                        $x + $left_line_width, $y + $line_width,);
468        $this->_canvas->polygon($points, $color, null, null, true);
469       
470        $points = array($x + $left - $left_line_width, $y + $top - $line_width,
471                        $x + $length - $right + $right_line_width, $y + $top - $line_width,
472                        $x + $length - $right, $y + $top,
473                        $x + $left, $y + $top);
474        $this->_canvas->polygon($points, $color, null, null, true);
475
476      } else {
477        $this->_canvas->filled_rectangle($x, $y, $length, $line_width, $color);
478        $this->_canvas->filled_rectangle($x, $y + $top - $line_width, $length, $line_width, $color);
479
480      }
481      break;
482     
483    case "bottom":
484      if ( $corner_style == "bevel" ) {
485        $left_line_width = $left / 4;
486        $right_line_width = $right / 4;
487       
488        $points = array($x, $y,
489                        $x + $length, $y,
490                        $x + $length - $right_line_width, $y - $line_width,
491                        $x + $left_line_width, $y - $line_width);
492        $this->_canvas->polygon($points, $color, null, null, true);
493       
494        $points = array($x + $left - $left_line_width, $y - $bottom + $line_width,
495                        $x + $length - $right + $right_line_width, $y - $bottom + $line_width,
496                        $x + $length - $right, $y - $bottom,
497                        $x + $left, $y - $bottom);
498        $this->_canvas->polygon($points, $color, null, null, true);
499
500      } else {
501        $this->_canvas->filled_rectangle($x, $y - $line_width, $length, $line_width, $color);
502        $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $line_width, $color);
503      }
504         
505      break;
506
507    case "left":
508      if ( $corner_style == "bevel" ) {
509        $top_line_width = $top / 4;
510        $bottom_line_width = $bottom / 4;
511       
512        $points = array($x, $y,
513                        $x, $y + $length,
514                        $x + $line_width, $y + $length - $bottom_line_width,
515                        $x + $line_width, $y + $top_line_width);
516        $this->_canvas->polygon($points, $color, null, null, true);
517
518        $points = array($x + $left - $line_width, $y + $top - $top_line_width,
519                        $x + $left - $line_width, $y + $length - $bottom + $bottom_line_width,
520                        $x + $left, $y + $length - $bottom,
521                        $x + $left, $y + $top);
522        $this->_canvas->polygon($points, $color, null, null, true);
523
524      } else {
525        $this->_canvas->filled_rectangle($x, $y, $line_width, $length, $color);
526        $this->_canvas->filled_rectangle($x + $left - $line_width, $y, $line_width, $length, $color);
527      }
528     
529      break;     
530                     
531    case "right":
532      if ( $corner_style == "bevel" ) {
533        $top_line_width = $top / 4;
534        $bottom_line_width = $bottom / 4;
535       
536     
537        $points = array($x, $y,
538                      $x, $y + $length,
539                        $x - $line_width, $y + $length - $bottom_line_width,
540                        $x - $line_width, $y + $top_line_width);
541        $this->_canvas->polygon($points, $color, null, null, true);
542       
543        $points = array($x - $right + $line_width, $y + $top - $top_line_width,
544                        $x - $right + $line_width, $y + $length - $bottom + $bottom_line_width,
545                        $x - $right, $y + $length - $bottom,
546                        $x - $right, $y + $top);
547        $this->_canvas->polygon($points, $color, null, null, true);
548
549      } else {
550        $this->_canvas->filled_rectangle($x - $line_width, $y, $line_width, $length, $color);
551        $this->_canvas->filled_rectangle($x - $right, $y, $line_width, $length, $color);
552      }
553     
554      break;
555
556    default:
557      return;
558
559    }
560       
561  }
562
563  protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
564    list($top, $right, $bottom, $left) = $widths;
565     
566    $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
567   
568    $this->_border_inset($x, $y, $length, $color, $half_widths, $side);
569
570    switch ($side) {
571
572    case "top":
573      $x += $left / 2;
574      $y += $top / 2;
575      $length -= $left / 2 + $right / 2;
576      break;
577
578    case "bottom":
579      $x += $left / 2;
580      $y -= $bottom / 2;
581      $length -= $left / 2 + $right / 2;
582      break;
583
584    case "left":
585      $x += $left / 2;
586      $y += $top / 2;
587      $length -= $top / 2 + $bottom / 2;
588      break;
589
590    case "right":
591      $x -= $right / 2;
592      $y += $top / 2;
593      $length -= $top / 2 + $bottom / 2;
594      break;
595
596    default:
597      return;
598
599    }
600
601    $this->_border_outset($x, $y, $length, $color, $half_widths, $side);
602   
603  }
604 
605  protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
606    list($top, $right, $bottom, $left) = $widths;
607     
608    $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
609   
610    $this->_border_outset($x, $y, $length, $color, $half_widths, $side);
611
612    switch ($side) {
613
614    case "top":
615      $x += $left / 2;
616      $y += $top / 2;
617      $length -= $left / 2 + $right / 2;
618      break;
619
620    case "bottom":
621      $x += $left / 2;
622      $y -= $bottom / 2;
623      $length -= $left / 2 + $right / 2;
624      break;
625
626    case "left":
627      $x += $left / 2;
628      $y += $top / 2;
629      $length -= $top / 2 + $bottom / 2;
630      break;
631
632    case "right":
633      $x -= $right / 2;
634      $y += $top / 2;
635      $length -= $top / 2 + $bottom / 2;
636      break;
637
638    default:
639      return;
640
641    }
642
643    $this->_border_inset($x, $y, $length, $color, $half_widths, $side);
644
645  }
646
647  protected function _tint($c) {
648    if ( !is_numeric($c) )
649      return $c;
650   
651    return min(1, $c + 0.66);
652  }
653
654  protected function _shade($c) {
655    if ( !is_numeric($c) )
656      return $c;
657   
658    return max(0, $c - 0.66);
659  }
660
661  protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
662    list($top, $right, $bottom, $left) = $widths;
663   
664    switch ($side) {
665
666    case "top":
667    case "left":
668      $shade = array_map(array($this, "_shade"), $color);
669      $this->_border_solid($x, $y, $length, $shade, $widths, $side);
670      break;
671
672    case "bottom":
673    case "right":
674      $tint = array_map(array($this, "_tint"), $color);     
675      $this->_border_solid($x, $y, $length, $tint, $widths, $side);
676      break;
677
678    default:
679      return;
680    }   
681  }
682 
683  protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
684    list($top, $right, $bottom, $left) = $widths;
685   
686    switch ($side) {
687    case "top":
688    case "left":
689      $tint = array_map(array($this, "_tint"), $color);
690      $this->_border_solid($x, $y, $length, $tint, $widths, $side);
691      break;
692
693    case "bottom":
694    case "right":
695      $shade = array_map(array($this, "_shade"), $color);
696      $this->_border_solid($x, $y, $length, $shade, $widths, $side);
697      break;
698
699    default:
700      return;
701
702    }   
703  }
704
705  //........................................................................
706 
707
708}
709
710?>
Note: See TracBrowser for help on using the repository browser.