4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Helmut Tischer <htischer@weihenstephan.org>
7 * @author Fabien Ménager <fabien.menager@gmail.com>
8 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
10 namespace Dompdf\Renderer
;
12 use Dompdf\Adapter\CPDF
;
18 use Dompdf\Image\Cache
;
25 abstract class AbstractRenderer
36 * Current dompdf instance
45 * @param Dompdf $dompdf The current dompdf instance
47 function __construct(Dompdf
$dompdf)
49 $this->_dompdf
= $dompdf;
50 $this->_canvas
= $dompdf->get_canvas();
56 * Specialized in child classes
58 * @param Frame $frame The frame to render
60 abstract function render(Frame
$frame);
62 //........................................................................
65 * Render a background image over a rectangular area
67 * @param string $url The background image to load
68 * @param float $x The left edge of the rectangular area
69 * @param float $y The top edge of the rectangular area
70 * @param float $width The width of the rectangular area
71 * @param float $height The height of the rectangular area
72 * @param Style $style The associated Style object
76 protected function _background_image($url, $x, $y, $width, $height, $style)
78 if (!function_exists("imagecreatetruecolor")) {
79 throw new \
Exception("The PHP GD extension is required, but is not installed.");
82 $sheet = $style->get_stylesheet();
84 // Skip degenerate cases
85 if ($width == 0 ||
$height == 0) {
90 $box_height = $height;
93 if ($this->_dompdf
->get_option("debugPng")) print '[_background_image ' . $url . ']';
95 list($img, $type, /*$msg*/) = Cache
::resolve_url(
97 $sheet->get_protocol(),
99 $sheet->get_base_path(),
103 // Bail if the image is no good
104 if (Cache
::is_broken($img)) {
108 //Try to optimize away reading and composing of same background multiple times
109 //Postponing read with imagecreatefrom ...()
110 //final composition parameters and name not known yet
111 //Therefore read dimension directly from file, instead of creating gd object first.
112 //$img_w = imagesx($src); $img_h = imagesy($src);
114 list($img_w, $img_h) = Helpers
::dompdf_getimagesize($img, $this->_dompdf
->getHttpContext());
115 if (!isset($img_w) ||
$img_w == 0 ||
!isset($img_h) ||
$img_h == 0) {
119 $repeat = $style->background_repeat
;
120 $dpi = $this->_dompdf
->get_option("dpi");
122 //Increase background resolution and dependent box size according to image resolution to be placed in
123 //Then image can be copied in without resize
124 $bg_width = round((float)($width * $dpi) / 72);
125 $bg_height = round((float)($height * $dpi) / 72);
127 //Need %bg_x, $bg_y as background pos, where img starts, converted to pixel
129 list($bg_x, $bg_y) = $style->background_position
;
131 if (Helpers
::is_percent($bg_x)) {
132 // The point $bg_x % from the left edge of the image is placed
133 // $bg_x % from the left edge of the background rectangle
134 $p = ((float)$bg_x) / 100.0;
136 $x2 = $p * $bg_width;
140 $bg_x = (float)($style->length_in_pt($bg_x) * $dpi) / 72;
143 $bg_x = round($bg_x +
$style->length_in_pt($style->border_left_width
) * $dpi / 72);
145 if (Helpers
::is_percent($bg_y)) {
146 // The point $bg_y % from the left edge of the image is placed
147 // $bg_y % from the left edge of the background rectangle
148 $p = ((float)$bg_y) / 100.0;
150 $y2 = $p * $bg_height;
154 $bg_y = (float)($style->length_in_pt($bg_y) * $dpi) / 72;
157 $bg_y = round($bg_y +
$style->length_in_pt($style->border_top_width
) * $dpi / 72);
159 //clip background to the image area on partial repeat. Nothing to do if img off area
160 //On repeat, normalize start position to the tile at immediate left/top or 0/0 of area
161 //On no repeat with positive offset: move size/start to have offset==0
162 //Handle x/y Dimensions separately
164 if ($repeat !== "repeat" && $repeat !== "repeat-x") {
167 $bg_width = $img_w +
$bg_x;
169 $x +
= ($bg_x * 72) / $dpi;
170 $bg_width = $bg_width - $bg_x;
171 if ($bg_width > $img_w) {
177 if ($bg_width <= 0) {
181 $width = (float)($bg_width * 72) / $dpi;
185 $bg_x = -((-$bg_x) %
$img_w);
187 $bg_x = $bg_x %
$img_w;
194 if ($repeat !== "repeat" && $repeat !== "repeat-y") {
197 $bg_height = $img_h +
$bg_y;
199 $y +
= ($bg_y * 72) / $dpi;
200 $bg_height = $bg_height - $bg_y;
201 if ($bg_height > $img_h) {
206 if ($bg_height <= 0) {
209 $height = (float)($bg_height * 72) / $dpi;
213 $bg_y = -((-$bg_y) %
$img_h);
215 $bg_y = $bg_y %
$img_h;
222 //Optimization, if repeat has no effect
223 if ($repeat === "repeat" && $bg_y <= 0 && $img_h +
$bg_y >= $bg_height) {
224 $repeat = "repeat-x";
227 if ($repeat === "repeat" && $bg_x <= 0 && $img_w +
$bg_x >= $bg_width) {
228 $repeat = "repeat-y";
231 if (($repeat === "repeat-x" && $bg_x <= 0 && $img_w +
$bg_x >= $bg_width) ||
232 ($repeat === "repeat-y" && $bg_y <= 0 && $img_h +
$bg_y >= $bg_height)
234 $repeat = "no-repeat";
237 //Use filename as indicator only
238 //different names for different variants to have different copies in the pdf
239 //This is not dependent of background color of box! .'_'.(is_array($bg_color) ? $bg_color["hex"] : $bg_color)
240 //Note: Here, bg_* are the start values, not end values after going through the tile loops!
245 $filedummy .= '_' . $bg_width . '_' . $bg_height . '_' . $bg_x . '_' . $bg_y . '_' . $repeat;
247 //Optimization to avoid multiple times rendering the same image.
248 //If check functions are existing and identical image already cached,
249 //then skip creation of duplicate, because it is not needed by addImagePng
250 if ($this->_canvas
instanceof CPDF
&&
251 $this->_canvas
->get_cpdf()->image_iscached($filedummy)
256 // Create a new image to fit over the background rectangle
257 $bg = imagecreatetruecolor($bg_width, $bg_height);
259 switch (strtolower($type)) {
262 imagesavealpha($bg, true);
263 imagealphablending($bg, false);
264 $src = imagecreatefrompng($img);
268 $src = imagecreatefromjpeg($img);
272 $src = imagecreatefromgif($img);
276 $src = Helpers
::imagecreatefrombmp($img);
280 return; // Unsupported image type
287 //Background color if box is not relevant here
288 //Non transparent image: box clipped to real size. Background non relevant.
289 //Transparent image: The image controls the transparency and lets shine through whatever background.
290 //However on transparent image preset the composed image with the transparency color,
291 //to keep the transparency when copying over the non transparent parts of the tiles.
292 $ti = imagecolortransparent($src);
295 $tc = imagecolorsforindex($src, $ti);
296 $ti = imagecolorallocate($bg, $tc['red'], $tc['green'], $tc['blue']);
297 imagefill($bg, 0, 0, $ti);
298 imagecolortransparent($bg, $ti);
301 //This has only an effect for the non repeatable dimension.
302 //compute start of src and dest coordinates of the single copy
319 //For historical reasons exchange meanings of variables:
320 //start_* will be the start values, while bg_* will be the temporary start values in the loops
324 // Copy regions from the source image to the background
325 if ($repeat === "no-repeat") {
327 // Simply place the image on the background
328 imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);
330 } else if ($repeat === "repeat-x") {
332 for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x +
= $img_w) {
342 imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
345 } else if ($repeat === "repeat-y") {
347 for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y +
= $img_h) {
357 imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
361 } else if ($repeat === "repeat") {
363 for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y +
= $img_h) {
364 for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x +
= $img_w) {
385 imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
389 print 'Unknown repeat!';
394 } /* End optimize away creation of duplicates */
396 $this->_canvas
->clipping_rectangle($x, $y, $box_width, $box_height);
398 //img: image url string
399 //img_w, img_h: original image size in px
400 //width, height: box size in pt
401 //bg_width, bg_height: box size in px
402 //x, y: left/top edge of box on page in pt
403 //start_x, start_y: placement of image relative to pattern
404 //$repeat: repeat mode
405 //$bg: GD object of result image
406 //$src: GD object of original image
407 //When using cpdf and optimization to direct png creation from gd object is available,
408 //don't create temp file, but place gd object directly into the pdf
409 if (!$is_png && $this->_canvas
instanceof CPDF
) {
410 // Note: CPDF_Adapter image converts y position
411 $this->_canvas
->get_cpdf()->addImagePng($filedummy, $x, $this->_canvas
->get_height() - $y - $height, $width, $height, $bg);
413 $tmp_dir = $this->_dompdf
->get_option("temp_dir");
414 $tmp_name = tempnam($tmp_dir, "bg_dompdf_img_");
416 $tmp_file = "$tmp_name.png";
419 if ($this->_dompdf
->get_option("debugPng")) print '[_background_image ' . $tmp_file . ']';
421 imagepng($bg, $tmp_file);
422 $this->_canvas
->image($tmp_file, $x, $y, $width, $height);
426 if ($this->_dompdf
->get_option("debugPng")) print '[_background_image unlink ' . $tmp_file . ']';
428 if (!$this->_dompdf
->get_option("debugKeepTemp")) {
433 $this->_canvas
->clipping_end();
436 protected function _get_dash_pattern($style, $width)
453 $pattern = array($width, $width * 2);
455 $pattern = array($width);
459 $pattern = array(3 * $width);
466 protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
471 protected function _border_hidden($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
476 // Border rendering functions
478 protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
480 $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "dotted", $r1, $r2);
484 protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
486 $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "dashed", $r1, $r2);
490 protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
492 // TODO: Solve rendering where one corner is beveled (radius == 0), one corner isn't.
493 if ($corner_style !== "bevel" ||
$r1 > 0 ||
$r2 > 0) {
494 // do it the simple way
495 $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "solid", $r1, $r2);
499 list($top, $right, $bottom, $left) = $widths;
501 // All this polygon business is for beveled corners...
504 $points = array($x, $y,
506 $x +
$length - $right, $y +
$top,
507 $x +
$left, $y +
$top);
508 $this->_canvas
->polygon($points, $color, null, null, true);
512 $points = array($x, $y,
514 $x +
$length - $right, $y - $bottom,
515 $x +
$left, $y - $bottom);
516 $this->_canvas
->polygon($points, $color, null, null, true);
520 $points = array($x, $y,
522 $x +
$left, $y +
$length - $bottom,
523 $x +
$left, $y +
$top);
524 $this->_canvas
->polygon($points, $color, null, null, true);
528 $points = array($x, $y,
530 $x - $right, $y +
$length - $bottom,
531 $x - $right, $y +
$top);
532 $this->_canvas
->polygon($points, $color, null, null, true);
540 protected function _apply_ratio($side, $ratio, $top, $right, $bottom, $left, &$x, &$y, &$length, &$r1, &$r2)
545 $r1 -= $left * $ratio;
546 $r2 -= $right * $ratio;
547 $x +
= $left * $ratio;
549 $length -= $left * $ratio +
$right * $ratio;
553 $r1 -= $right * $ratio;
554 $r2 -= $left * $ratio;
555 $x +
= $left * $ratio;
556 $y -= $bottom * $ratio;
557 $length -= $left * $ratio +
$right * $ratio;
561 $r1 -= $top * $ratio;
562 $r2 -= $bottom * $ratio;
563 $x +
= $left * $ratio;
565 $length -= $top * $ratio +
$bottom * $ratio;
569 $r1 -= $bottom * $ratio;
570 $r2 -= $top * $ratio;
571 $x -= $right * $ratio;
573 $length -= $top * $ratio +
$bottom * $ratio;
582 protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
584 list($top, $right, $bottom, $left) = $widths;
586 $third_widths = array($top / 3, $right / 3, $bottom / 3, $left / 3);
588 // draw the outer border
589 $this->_border_solid($x, $y, $length, $color, $third_widths, $side, $corner_style, $r1, $r2);
591 $this->_apply_ratio($side, 2 / 3, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
593 $this->_border_solid($x, $y, $length, $color, $third_widths, $side, $corner_style, $r1, $r2);
596 protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
598 list($top, $right, $bottom, $left) = $widths;
600 $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
602 $this->_border_inset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
604 $this->_apply_ratio($side, 0.5, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
606 $this->_border_outset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
610 protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
612 list($top, $right, $bottom, $left) = $widths;
614 $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
616 $this->_border_outset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
618 $this->_apply_ratio($side, 0.5, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
620 $this->_border_inset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
624 protected function _tint($c)
629 return min(1, $c +
0.16);
632 protected function _shade($c)
637 return max(0, $c - 0.33);
640 protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
645 $shade = array_map(array($this, "_shade"), $color);
646 $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
651 $tint = array_map(array($this, "_tint"), $color);
652 $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
660 protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
665 $tint = array_map(array($this, "_tint"), $color);
666 $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
671 $shade = array_map(array($this, "_shade"), $color);
672 $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
680 // Draws a solid, dotted, or dashed line, observing the border radius
681 protected function _border_line($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $pattern_name, $r1 = 0, $r2 = 0)
683 list($top, $right, $bottom, $left) = $widths;
686 $pattern = $this->_get_dash_pattern($pattern_name, $width);
688 $half_width = $width / 2;
700 $this->_canvas
->arc($x +
$r1, $y +
$r1, $r1, $r1, 90 - $adjust, 135 +
$adjust, $color, $width, $pattern);
703 $this->_canvas
->line($x +
$r1, $y, $x +
$length - $r2, $y, $color, $width, $pattern);
706 $this->_canvas
->arc($x +
$length - $r2, $y +
$r2, $r2, $r2, 45 - $adjust, 90 +
$adjust, $color, $width, $pattern);
715 $this->_canvas
->arc($x +
$r1, $y - $r1, $r1, $r1, 225 - $adjust, 270 +
$adjust, $color, $width, $pattern);
718 $this->_canvas
->line($x +
$r1, $y, $x +
$length - $r2, $y, $color, $width, $pattern);
721 $this->_canvas
->arc($x +
$length - $r2, $y - $r2, $r2, $r2, 270 - $adjust, 315 +
$adjust, $color, $width, $pattern);
730 $this->_canvas
->arc($x +
$r1, $y +
$r1, $r1, $r1, 135 - $adjust, 180 +
$adjust, $color, $width, $pattern);
733 $this->_canvas
->line($x, $y +
$r1, $x, $y +
$length - $r2, $color, $width, $pattern);
736 $this->_canvas
->arc($x +
$r2, $y +
$length - $r2, $r2, $r2, 180 - $adjust, 225 +
$adjust, $color, $width, $pattern);
745 $this->_canvas
->arc($x - $r1, $y +
$r1, $r1, $r1, 0 - $adjust, 45 +
$adjust, $color, $width, $pattern);
748 $this->_canvas
->line($x, $y +
$r1, $x, $y +
$length - $r2, $color, $width, $pattern);
751 $this->_canvas
->arc($x - $r2, $y +
$length - $r2, $r2, $r2, 315 - $adjust, 360 +
$adjust, $color, $width, $pattern);
757 protected function _set_opacity($opacity)
759 if (is_numeric($opacity) && $opacity <= 1.0 && $opacity >= 0.0) {
760 $this->_canvas
->set_opacity($opacity);
764 protected function _debug_layout($box, $color = "red", $style = array())
766 $this->_canvas
->rectangle($box[0], $box[1], $box[2], $box[3], Color
::parse($color), 0.1, $style);