PageRenderTime 55ms CodeModel.GetById 13ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 0ms

/dompdf/include/abstract_renderer.cls.php

http://findresources.googlecode.com/
PHP | 890 lines | 534 code | 169 blank | 187 comment | 110 complexity | 67cdec1b98902b5f6c1fb54dc41db428 MD5 | raw file
  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.dompdf.com/
 32 *
 33 * @link http://www.dompdf.com/
 34 * @copyright 2004 Benj Carson
 35 * @author Benj Carson <benjcarson@digitaljunkies.ca>
 36 * @contributor Helmut Tischer <htischer@weihenstephan.org>
 37 * @package dompdf
 38 *
 39 * Changes
 40 * @contributor Helmut Tischer <htischer@weihenstephan.org>
 41 * @version 0.5.1.htischer.20090507
 42 * - On background image
 43 *   - Clip invisible areas from background images, then merge identical
 44 *     image/size/offset to a single image.
 45 *   - Fix rounding of background image size.
 46 *   - Fix background image position given as percent
 47 *   - Check if identical image is already cached by cpdf. Then do not create
 48 *     duplicates to save memory and CPU time
 49 *   - Fix skipping of image repetition if area is too small
 50 *   - Do not create temporary files, but pass gd object directly
 51 */
 52
 53/* $Id: abstract_renderer.cls.php 361 2011-02-16 21:03:05Z fabien.menager $ */
 54
 55/**
 56 * Base renderer class
 57 *
 58 * @access private
 59 * @package dompdf
 60 */
 61abstract class Abstract_Renderer {
 62
 63  /**
 64   * Rendering backend
 65   *
 66   * @var Canvas
 67   */
 68  protected $_canvas;
 69
 70  /**
 71   * Current dompdf instance
 72   *
 73   * @var DOMPDF
 74   */
 75  protected $_dompdf;
 76  
 77  /**
 78   * Class constructor
 79   *
 80   * @param DOMPDF $dompdf The current dompdf instance
 81   */
 82  function __construct(DOMPDF $dompdf) {
 83    $this->_dompdf = $dompdf;
 84    $this->_canvas = $dompdf->get_canvas();
 85  }
 86  
 87  /**
 88   * Render a frame.
 89   *
 90   * Specialized in child classes
 91   *
 92   * @param Frame $frame The frame to render
 93   */
 94  abstract function render(Frame $frame);
 95
 96  //........................................................................
 97
 98  /**
 99   * Render a background image over a rectangular area
100   *
101   * @param string $img      The background image to load
102   * @param float  $x        The left edge of the rectangular area
103   * @param float  $y        The top edge of the rectangular area
104   * @param float  $width    The width of the rectangular area
105   * @param float  $height   The height of the rectangular area
106   * @param Style  $style    The associated Style object
107   */
108  protected function _background_image($url, $x, $y, $width, $height, $style) {
109    $sheet = $style->get_stylesheet();
110
111    // Skip degenerate cases
112    if ( $width == 0 || $height == 0 )
113      return;
114
115    //debugpng
116    if (DEBUGPNG) print '[_background_image '.$url.']';
117
118    list($img, $ext) = Image_Cache::resolve_url($url,
119                                                $sheet->get_protocol(),
120                                                $sheet->get_host(),
121                                                $sheet->get_base_path());
122
123    // Bail if the image is no good
124    if ( $img === DOMPDF_LIB_DIR . "/res/broken_image.png" )
125      return;
126
127    //Try to optimize away reading and composing of same background multiple times
128    //Postponing read with imagecreatefrom   ...()
129    //final composition paramters and name not known yet
130    //Therefore read dimension directly from file, instead of creating gd object first.
131    //$img_w = imagesx($src); $img_h = imagesy($src);
132
133    list($img_w, $img_h) = dompdf_getimagesize($img);
134    if (!isset($img_w) || $img_w == 0 || !isset($img_h) || $img_h == 0) {
135      return;
136    }
137
138    $repeat = $style->background_repeat;
139    $bg_color = $style->background_color;
140
141    //Increase background resolution and dependent box size according to image resolution to be placed in
142    //Then image can be copied in without resize
143    $bg_width = round((float)($width * DOMPDF_DPI) / 72);
144    $bg_height = round((float)($height * DOMPDF_DPI) / 72);
145
146    //Need %bg_x, $bg_y as background pos, where img starts, converted to pixel
147
148    list($bg_x, $bg_y) = $style->background_position;
149
150    if ( is_percent($bg_x) ) {
151      // The point $bg_x % from the left edge of the image is placed
152      // $bg_x % from the left edge of the background rectangle
153      $p = ((float)$bg_x)/100.0;
154      $x1 = $p * $img_w;
155      $x2 = $p * $bg_width;
156
157      $bg_x = round($x2 - $x1);
158    } else {
159      $bg_x = round((float)($style->length_in_pt($bg_x)*DOMPDF_DPI) / 72);
160    }
161
162    if ( is_percent($bg_y) ) {
163      // The point $bg_y % from the left edge of the image is placed
164      // $bg_y % from the left edge of the background rectangle
165      $p = ((float)$bg_y)/100.0;
166      $y1 = $p * $img_h;
167      $y2 = $p * $bg_height;
168
169      $bg_y = round($y2 - $y1);
170    } else {
171      $bg_y = round((float)($style->length_in_pt($bg_y)*DOMPDF_DPI) / 72);
172    }
173
174    //clip background to the image area on partial repeat. Nothing to do if img off area
175    //On repeat, normalize start position to the tile at immediate left/top or 0/0 of area
176    //On no repeat with positive offset: move size/start to have offset==0
177    //Handle x/y Dimensions separately
178
179    if ( $repeat !== "repeat" && $repeat !== "repeat-x" ) {
180      //No repeat x
181      if ($bg_x < 0) {
182        $bg_width = $img_w + $bg_x;
183      } else {
184        $x += ($bg_x * 72)/DOMPDF_DPI;
185        $bg_width = $bg_width - $bg_x;
186        if ($bg_width > $img_w) {
187          $bg_width = $img_w;
188        }
189        $bg_x = 0;
190      }
191      if ($bg_width <= 0) {
192          return;
193      }
194      $width = (float)($bg_width * 72)/DOMPDF_DPI;
195    } else {
196      //repeat x
197      if ($bg_x < 0) {
198        $bg_x = - ((-$bg_x) % $img_w);
199      } else {
200        $bg_x = $bg_x % $img_w;
201        if ($bg_x > 0) {
202          $bg_x -= $img_w;
203        }
204      }
205    }
206
207    if ( $repeat !== "repeat" && $repeat !== "repeat-y" ) {
208      //no repeat y
209      if ($bg_y < 0) {
210        $bg_height = $img_h + $bg_y;
211      } else {
212        $y += ($bg_y * 72)/DOMPDF_DPI;
213        $bg_height = $bg_height - $bg_y;
214        if ($bg_height > $img_h) {
215          $bg_height = $img_h;
216        }
217        $bg_y = 0;
218      }
219      if ($bg_height <= 0) {
220          return;
221      }
222      $height = (float)($bg_height * 72)/DOMPDF_DPI;
223    } else {
224      //repeat y
225      if ($bg_y < 0) {
226        $bg_y = - ((-$bg_y) % $img_h);
227      } else {
228        $bg_y = $bg_y % $img_h;
229        if ($bg_y > 0) {
230          $bg_y -= $img_h;
231        }
232      }
233    }
234
235    //Optimization, if repeat has no effect
236    if ( $repeat === "repeat" && $bg_y <= 0 && $img_h+$bg_y >= $bg_height ) {
237      $repeat = "repeat-x";
238    }
239    if ( $repeat === "repeat" && $bg_x <= 0 && $img_w+$bg_x >= $bg_width ) {
240      $repeat = "repeat-y";
241    }
242    if ( ($repeat === "repeat-x" && $bg_x <= 0 && $img_w+$bg_x >= $bg_width) ||
243         ($repeat === "repeat-y" && $bg_y <= 0 && $img_h+$bg_y >= $bg_height) ) {
244      $repeat = "no-repeat";
245    }
246
247    //Use filename as indicator only
248    //different names for different variants to have different copies in the pdf
249    //This is not dependent of background color of box! .'_'.(is_array($bg_color) ? $bg_color["hex"] : $bg_color)
250    //Note: Here, bg_* are the start values, not end values after going through the tile loops!
251
252    $filedummy = $img;
253
254    /* 
255    //Make shorter strings with limited characters for cache associative array index - needed?    
256    //Strip common base path - server root, explicite temp, default temp; remove unwanted characters;
257    $filedummy = strtr($filedummy,"\\:","//");
258    $p = strtr($_SERVER["DOCUMENT_ROOT"],"\\:","//");
259    $l = strlen($p);
260    if ( substr($filedummy,0,$l) == $p) {
261      $filedummy = substr($filedummy,$l);
262    } else {
263      $p = strtr(DOMPDF_TEMP_DIR,"\\:","//");
264      $l = strlen($p);
265      if ( substr($filedummy,0,$l) == $p) {
266        $filedummy = substr($filedummy,$l);
267      } else {
268        $p = strtr(sys_get_temp_dir(),"\\:","//");
269        $l = strlen($p);
270        if ( substr($filedummy,0,$l) == $p) {
271          $filedummy = substr($filedummy,$l);
272        }
273      }
274    }
275    */
276    
277    $filedummy .= '_'.$bg_width.'_'.$bg_height.'_'.$bg_x.'_'.$bg_y.'_'.$repeat;
278    //debugpng
279    //if (DEBUGPNG) print '<pre>[_background_image name '.$filedummy.']</pre>';
280
281    //Optimization to avoid multiple times rendering the same image.
282    //If check functions are existing and identical image already cached,
283    //then skip creation of duplicate, because it is not needed by addImagePng
284    if ( method_exists( $this->_canvas, "get_cpdf" ) &&
285         method_exists( $this->_canvas->get_cpdf(), "addImagePng" ) &&
286         method_exists( $this->_canvas->get_cpdf(), "image_iscached" ) &&
287         $this->_canvas->get_cpdf()->image_iscached($filedummy) ) {
288       $bg = null;
289
290      //debugpng
291      //if (DEBUGPNG) print '[_background_image skip]';
292    } 
293    
294    else {
295
296    // Create a new image to fit over the background rectangle
297    $bg = imagecreatetruecolor($bg_width, $bg_height);
298    
299    //anyway default
300    //imagealphablending($img, true);
301
302    switch (strtolower($ext)) {
303      case "png":
304        $src = imagecreatefrompng($img);
305        break;
306  
307      case "jpg":
308      case "jpeg":
309        $src = imagecreatefromjpeg($img);
310        break;
311  
312      case "gif":
313        $src = imagecreatefromgif($img);
314        break;
315        
316      case "bmp":
317        $src = imagecreatefrombmp($img);
318        break;
319  
320      default: return; // Unsupported image type
321    }
322
323    if ($src == null) {
324      return;
325    }
326
327    //Background color if box is not relevant here
328    //Non transparent image: box clipped to real size. Background non relevant.
329    //Transparent image: The image controls the transparency and lets shine through whatever background.
330    //However on transparent imaage preset the composed image with the transparency color,
331    //to keep the transparency when copying over the non transparent parts of the tiles.
332    $ti = imagecolortransparent($src);
333    
334    if ($ti >= 0) {
335      $tc = imagecolorsforindex($src,$ti);
336      $ti = imagecolorallocate($bg,$tc['red'],$tc['green'],$tc['blue']);
337      imagefill($bg,0,0,$ti);
338      imagecolortransparent($bg, $ti);
339    }
340
341    //This has only an effect for the non repeatable dimension.
342    //compute start of src and dest coordinates of the single copy
343    if ( $bg_x < 0 ) {
344      $dst_x = 0;
345      $src_x = -$bg_x;
346    } else {
347      $src_x = 0;
348      $dst_x = $bg_x;
349    }
350
351    if ( $bg_y < 0 ) {
352      $dst_y = 0;
353      $src_y = -$bg_y;
354    } else {
355      $src_y = 0;
356      $dst_y = $bg_y;
357    }
358
359    //For historical reasons exchange meanings of variables:
360    //start_* will be the start values, while bg_* will be the temporary start values in the loops
361    $start_x = $bg_x;
362    $start_y = $bg_y;
363
364    // Copy regions from the source image to the background
365    if ( $repeat === "no-repeat" ) {
366
367      // Simply place the image on the background
368      imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);
369
370    } else if ( $repeat === "repeat-x" ) {
371
372      for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
373        if ( $bg_x < 0 ) {
374          $dst_x = 0;
375          $src_x = -$bg_x;
376          $w = $img_w + $bg_x;
377        } else {
378          $dst_x = $bg_x;
379          $src_x = 0;
380          $w = $img_w;
381        }
382        imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
383      }
384
385    } else if ( $repeat === "repeat-y" ) {
386
387      for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
388        if ( $bg_y < 0 ) {
389          $dst_y = 0;
390          $src_y = -$bg_y;
391          $h = $img_h + $bg_y;
392        } else {
393          $dst_y = $bg_y;
394          $src_y = 0;
395          $h = $img_h;
396        }
397        imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
398
399      }
400
401    } else if ( $repeat === "repeat" ) {
402
403      for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
404        for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
405
406          if ( $bg_x < 0 ) {
407            $dst_x = 0;
408            $src_x = -$bg_x;
409            $w = $img_w + $bg_x;
410          } else {
411            $dst_x = $bg_x;
412            $src_x = 0;
413            $w = $img_w;
414          }
415
416          if ( $bg_y < 0 ) {
417            $dst_y = 0;
418            $src_y = -$bg_y;
419            $h = $img_h + $bg_y;
420          } else {
421            $dst_y = $bg_y;
422            $src_y = 0;
423            $h = $img_h;
424          }
425          imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
426        }
427      }
428    }
429    
430    else {
431      print 'Unknown repeat!';
432    }
433    
434    imagedestroy($src);
435
436    } /* End optimize away creation of duplicates */
437
438    //img: image url string
439    //img_w, img_h: original image size in px
440    //width, height: box size in pt
441    //bg_width, bg_height: box size in px
442    //x, y: left/top edge of box on page in pt
443    //start_x, start_y: placement of image relativ to pattern
444    //$repeat: repeat mode
445    //$bg: GD object of result image
446    //$src: GD object of original image
447    //When using cpdf and optimization to direct png creation from gd object is available,
448    //don't create temp file, but place gd object directly into the pdf
449    if ( method_exists( $this->_canvas, "get_cpdf" ) && 
450         method_exists( $this->_canvas->get_cpdf(), "addImagePng" ) ) {
451      // Note: CPDF_Adapter image converts y position
452      $this->_canvas->get_cpdf()->addImagePng($filedummy, $x, $this->_canvas->get_height() - $y - $height, $width, $height, $bg);
453    } 
454    
455    else {
456      $tmp_file = tempnam(DOMPDF_TEMP_DIR, "bg_dompdf_img_").'.png';
457      //debugpng
458      if (DEBUGPNG) print '[_background_image '.$tmp_file.']';
459
460      imagepng($bg, $tmp_file);
461      $this->_canvas->image($tmp_file, "png", $x, $y, $width, $height);
462      imagedestroy($bg);
463
464      //debugpng
465      if (DEBUGPNG) print '[_background_image unlink '.$tmp_file.']';
466
467      if (!DEBUGKEEPTEMP)
468        unlink($tmp_file);
469    }
470  }
471  
472  protected function _get_dash_pattern($style, $width) {
473    $pattern = array();
474    
475    switch ($style) {
476      default:
477      /*case "solid":
478      case "double":
479      case "groove":
480      case "inset":
481      case "outset":
482      case "ridge":*/
483      case "none": break;
484      
485      case "dotted": 
486        if ( $width < 2 )
487          $pattern = array($width, 2);
488        else
489          $pattern = array($width);
490      break;
491      
492      case "dashed": 
493        $pattern = array(3 * $width);
494      break;
495    }
496    
497    return $pattern;
498  }
499
500  protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
501    return;
502  }
503  
504  // Border rendering functions
505  protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
506    list($top, $right, $bottom, $left) = $widths;
507
508    $pattern = $this->_get_dash_pattern("dotted", $$side);
509    
510    switch ($side) {
511
512    case "top":
513      $delta = $top / 2;
514    case "bottom":
515      $delta = isset($delta) ? $delta : -$bottom / 2;
516      $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, $pattern);
517      break;
518
519    case "left":
520      $delta = $left / 2;
521    case "right":
522      $delta = isset($delta) ? $delta : - $right / 2;
523      $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, $pattern);
524      break;
525
526    default:
527      return;
528
529    }
530  }
531
532  
533  protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
534    list($top, $right, $bottom, $left) = $widths;
535
536    $pattern = $this->_get_dash_pattern("dashed", $$side);
537    
538    switch ($side) {
539
540    case "top":
541      $delta = $top / 2;
542    case "bottom":
543      $delta = isset($delta) ? $delta : -$bottom / 2;
544      $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, $pattern);
545      break;
546
547    case "left":
548      $delta = $left / 2;
549    case "right":
550      $delta = isset($delta) ? $delta : - $right / 2;
551      $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, $pattern);
552      break;
553
554    default:
555      return;
556    }
557    
558  }
559
560  
561  protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
562    list($top, $right, $bottom, $left) = $widths;
563
564    // All this polygon business is for beveled corners...
565    switch ($side) {
566
567    case "top":
568      if ( $corner_style === "bevel" ) {
569        
570        $points = array($x, $y,
571                        $x + $length, $y,
572                        $x + $length - $right, $y + $top,
573                        $x + $left, $y + $top);
574        $this->_canvas->polygon($points, $color, null, null, true);
575      } else
576        $this->_canvas->filled_rectangle($x, $y, $length, $top, $color);
577      
578      break;
579      
580    case "bottom":
581      if ( $corner_style === "bevel" ) {
582        $points = array($x, $y,
583                        $x + $length, $y,
584                        $x + $length - $right, $y - $bottom,
585                        $x + $left, $y - $bottom);
586        $this->_canvas->polygon($points, $color, null, null, true);
587      } else
588        $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $bottom, $color);
589      
590      break;
591      
592    case "left":
593      if ( $corner_style === "bevel" ) {
594        $points = array($x, $y,
595                        $x, $y + $length,
596                        $x + $left, $y + $length - $bottom,
597                        $x + $left, $y + $top);
598        $this->_canvas->polygon($points, $color, null, null, true);
599      } else
600        $this->_canvas->filled_rectangle($x, $y, $left, $length, $color);
601      
602      break;
603      
604    case "right":
605      if ( $corner_style === "bevel" ) {
606        $points = array($x, $y,
607                        $x, $y + $length,
608                        $x - $right, $y + $length - $bottom,
609                        $x - $right, $y + $top);
610        $this->_canvas->polygon($points, $color, null, null, true);
611      } else
612        $this->_canvas->filled_rectangle($x - $right, $y, $right, $length, $color);
613
614      break;
615
616    default:
617      return;
618
619    }
620        
621  }
622
623
624  protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
625    list($top, $right, $bottom, $left) = $widths;
626    
627    $line_width = $$side / 4;
628    
629    // We draw the outermost edge first. Points are ordered: outer left,
630    // outer right, inner right, inner left, or outer top, outer bottom,
631    // inner bottom, inner top.
632    switch ($side) {
633
634    case "top":
635      if ( $corner_style === "bevel" ) {
636        $left_line_width = $left / 4;
637        $right_line_width = $right / 4;
638        
639        $points = array($x, $y,
640                        $x + $length, $y,
641                        $x + $length - $right_line_width, $y + $line_width,
642                        $x + $left_line_width, $y + $line_width,);
643        $this->_canvas->polygon($points, $color, null, null, true);
644        
645        $points = array($x + $left - $left_line_width, $y + $top - $line_width,
646                        $x + $length - $right + $right_line_width, $y + $top - $line_width,
647                        $x + $length - $right, $y + $top,
648                        $x + $left, $y + $top);
649        $this->_canvas->polygon($points, $color, null, null, true);
650
651      } else {
652        $this->_canvas->filled_rectangle($x, $y, $length, $line_width, $color);
653        $this->_canvas->filled_rectangle($x, $y + $top - $line_width, $length, $line_width, $color);
654
655      }
656      break;
657      
658    case "bottom":
659      if ( $corner_style === "bevel" ) {
660        $left_line_width = $left / 4;
661        $right_line_width = $right / 4;
662        
663        $points = array($x, $y,
664                        $x + $length, $y,
665                        $x + $length - $right_line_width, $y - $line_width,
666                        $x + $left_line_width, $y - $line_width);
667        $this->_canvas->polygon($points, $color, null, null, true);
668        
669        $points = array($x + $left - $left_line_width, $y - $bottom + $line_width,
670                        $x + $length - $right + $right_line_width, $y - $bottom + $line_width,
671                        $x + $length - $right, $y - $bottom,
672                        $x + $left, $y - $bottom);
673        $this->_canvas->polygon($points, $color, null, null, true);
674
675      } else {
676        $this->_canvas->filled_rectangle($x, $y - $line_width, $length, $line_width, $color);
677        $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $line_width, $color);
678      }
679          
680      break;
681
682    case "left":
683      if ( $corner_style === "bevel" ) {
684        $top_line_width = $top / 4;
685        $bottom_line_width = $bottom / 4;
686        
687        $points = array($x, $y,
688                        $x, $y + $length,
689                        $x + $line_width, $y + $length - $bottom_line_width,
690                        $x + $line_width, $y + $top_line_width);
691        $this->_canvas->polygon($points, $color, null, null, true);
692
693        $points = array($x + $left - $line_width, $y + $top - $top_line_width,
694                        $x + $left - $line_width, $y + $length - $bottom + $bottom_line_width,
695                        $x + $left, $y + $length - $bottom,
696                        $x + $left, $y + $top);
697        $this->_canvas->polygon($points, $color, null, null, true);
698
699      } else {
700        $this->_canvas->filled_rectangle($x, $y, $line_width, $length, $color);
701        $this->_canvas->filled_rectangle($x + $left - $line_width, $y, $line_width, $length, $color);
702      }
703      
704      break;
705                      
706    case "right":
707      if ( $corner_style === "bevel" ) {
708        $top_line_width = $top / 4;
709        $bottom_line_width = $bottom / 4;
710        
711      
712        $points = array($x, $y,
713                      $x, $y + $length,
714                        $x - $line_width, $y + $length - $bottom_line_width,
715                        $x - $line_width, $y + $top_line_width);
716        $this->_canvas->polygon($points, $color, null, null, true);
717        
718        $points = array($x - $right + $line_width, $y + $top - $top_line_width,
719                        $x - $right + $line_width, $y + $length - $bottom + $bottom_line_width,
720                        $x - $right, $y + $length - $bottom,
721                        $x - $right, $y + $top);
722        $this->_canvas->polygon($points, $color, null, null, true);
723
724      } else {
725        $this->_canvas->filled_rectangle($x - $line_width, $y, $line_width, $length, $color);
726        $this->_canvas->filled_rectangle($x - $right, $y, $line_width, $length, $color);
727      }
728      
729      break;
730
731    default:
732      return;
733
734    }
735        
736  }
737
738  protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
739    list($top, $right, $bottom, $left) = $widths;
740      
741    $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
742    
743    $this->_border_inset($x, $y, $length, $color, $half_widths, $side);
744
745    switch ($side) {
746
747    case "top":
748      $x += $left / 2;
749      $y += $top / 2;
750      $length -= $left / 2 + $right / 2;
751      break;
752
753    case "bottom":
754      $x += $left / 2;
755      $y -= $bottom / 2;
756      $length -= $left / 2 + $right / 2;
757      break;
758
759    case "left":
760      $x += $left / 2;
761      $y += $top / 2;
762      $length -= $top / 2 + $bottom / 2;
763      break;
764
765    case "right":
766      $x -= $right / 2;
767      $y += $top / 2;
768      $length -= $top / 2 + $bottom / 2;
769      break;
770
771    default:
772      return;
773
774    }
775
776    $this->_border_outset($x, $y, $length, $color, $half_widths, $side);
777    
778  }
779  
780  protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
781    list($top, $right, $bottom, $left) = $widths;
782     
783    $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
784    
785    $this->_border_outset($x, $y, $length, $color, $half_widths, $side);
786
787    switch ($side) {
788
789    case "top":
790      $x += $left / 2;
791      $y += $top / 2;
792      $length -= $left / 2 + $right / 2;
793      break;
794
795    case "bottom":
796      $x += $left / 2;
797      $y -= $bottom / 2;
798      $length -= $left / 2 + $right / 2;
799      break;
800
801    case "left":
802      $x += $left / 2;
803      $y += $top / 2;
804      $length -= $top / 2 + $bottom / 2;
805      break;
806
807    case "right":
808      $x -= $right / 2;
809      $y += $top / 2;
810      $length -= $top / 2 + $bottom / 2;
811      break;
812
813    default:
814      return;
815
816    }
817
818    $this->_border_inset($x, $y, $length, $color, $half_widths, $side);
819
820  }
821
822  protected function _tint($c) {
823    if ( !is_numeric($c) )
824      return $c;
825    
826    return min(1, $c + 0.16);
827  }
828
829  protected function _shade($c) {
830    if ( !is_numeric($c) )
831      return $c;
832    
833    return max(0, $c - 0.33);
834  }
835
836  protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
837    list($top, $right, $bottom, $left) = $widths;
838    
839    switch ($side) {
840
841    case "top":
842    case "left":
843      $shade = array_map(array($this, "_shade"), $color);
844      $this->_border_solid($x, $y, $length, $shade, $widths, $side);
845      break;
846
847    case "bottom":
848    case "right":
849      $tint = array_map(array($this, "_tint"), $color);
850      $this->_border_solid($x, $y, $length, $tint, $widths, $side);
851      break;
852
853    default:
854      return;
855    }
856  }
857  
858  protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
859    list($top, $right, $bottom, $left) = $widths;
860    
861    switch ($side) {
862    case "top":
863    case "left":
864      $tint = array_map(array($this, "_tint"), $color);
865      $this->_border_solid($x, $y, $length, $tint, $widths, $side);
866      break;
867
868    case "bottom":
869    case "right":
870      $shade = array_map(array($this, "_shade"), $color);
871      $this->_border_solid($x, $y, $length, $shade, $widths, $side);
872      break;
873
874    default:
875      return;
876
877    }
878  }
879  
880  protected function _set_opacity($opacity) {
881    if ( is_numeric($opacity) && $opacity <= 1.0 && $opacity >= 0.0 ) {
882      $this->_canvas->set_opacity( $opacity );
883    }
884  }
885  
886  protected function _debug_layout($box, $color = "red", $style = array()) {
887    $this->_canvas->rectangle($box[0], $box[1], $box[2], $box[3], CSS_Color::parse($color), 0.1, $style);
888  }
889  //........................................................................
890}