Install zendframework via composer, update build.xml and run, updates to composer
[openemr.git] / vendor / dompdf / dompdf / src / Renderer / AbstractRenderer.php
blob31b595fd430e54e6501aa1e3e7fde3b8e9ee5724
1 <?php
2 /**
3 * @package dompdf
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
9 */
10 namespace Dompdf\Renderer;
12 use Dompdf\Adapter\CPDF;
13 use Dompdf\Css\Color;
14 use Dompdf\Css\Style;
15 use Dompdf\Dompdf;
16 use Dompdf\Helpers;
17 use Dompdf\Frame;
18 use Dompdf\Image\Cache;
20 /**
21 * Base renderer class
23 * @package dompdf
25 abstract class AbstractRenderer
28 /**
29 * Rendering backend
31 * @var \Dompdf\Canvas
33 protected $_canvas;
35 /**
36 * Current dompdf instance
38 * @var Dompdf
40 protected $_dompdf;
42 /**
43 * Class constructor
45 * @param Dompdf $dompdf The current dompdf instance
47 function __construct(Dompdf $dompdf)
49 $this->_dompdf = $dompdf;
50 $this->_canvas = $dompdf->get_canvas();
53 /**
54 * Render a frame.
56 * Specialized in child classes
58 * @param Frame $frame The frame to render
60 abstract function render(Frame $frame);
62 //........................................................................
64 /**
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
74 * @throws \Exception
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) {
86 return;
89 $box_width = $width;
90 $box_height = $height;
92 //debugpng
93 if ($this->_dompdf->get_option("debugPng")) print '[_background_image ' . $url . ']';
95 list($img, $type, /*$msg*/) = Cache::resolve_url(
96 $url,
97 $sheet->get_protocol(),
98 $sheet->get_host(),
99 $sheet->get_base_path(),
100 $this->_dompdf
103 // Bail if the image is no good
104 if (Cache::is_broken($img)) {
105 return;
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) {
116 return;
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;
135 $x1 = $p * $img_w;
136 $x2 = $p * $bg_width;
138 $bg_x = $x2 - $x1;
139 } else {
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;
149 $y1 = $p * $img_h;
150 $y2 = $p * $bg_height;
152 $bg_y = $y2 - $y1;
153 } else {
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") {
165 //No repeat x
166 if ($bg_x < 0) {
167 $bg_width = $img_w + $bg_x;
168 } else {
169 $x += ($bg_x * 72) / $dpi;
170 $bg_width = $bg_width - $bg_x;
171 if ($bg_width > $img_w) {
172 $bg_width = $img_w;
174 $bg_x = 0;
177 if ($bg_width <= 0) {
178 return;
181 $width = (float)($bg_width * 72) / $dpi;
182 } else {
183 //repeat x
184 if ($bg_x < 0) {
185 $bg_x = -((-$bg_x) % $img_w);
186 } else {
187 $bg_x = $bg_x % $img_w;
188 if ($bg_x > 0) {
189 $bg_x -= $img_w;
194 if ($repeat !== "repeat" && $repeat !== "repeat-y") {
195 //no repeat y
196 if ($bg_y < 0) {
197 $bg_height = $img_h + $bg_y;
198 } else {
199 $y += ($bg_y * 72) / $dpi;
200 $bg_height = $bg_height - $bg_y;
201 if ($bg_height > $img_h) {
202 $bg_height = $img_h;
204 $bg_y = 0;
206 if ($bg_height <= 0) {
207 return;
209 $height = (float)($bg_height * 72) / $dpi;
210 } else {
211 //repeat y
212 if ($bg_y < 0) {
213 $bg_y = -((-$bg_y) % $img_h);
214 } else {
215 $bg_y = $bg_y % $img_h;
216 if ($bg_y > 0) {
217 $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!
242 $filedummy = $img;
244 $is_png = false;
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)
253 $bg = null;
254 } else {
256 // Create a new image to fit over the background rectangle
257 $bg = imagecreatetruecolor($bg_width, $bg_height);
259 switch (strtolower($type)) {
260 case "png":
261 $is_png = true;
262 imagesavealpha($bg, true);
263 imagealphablending($bg, false);
264 $src = imagecreatefrompng($img);
265 break;
267 case "jpeg":
268 $src = imagecreatefromjpeg($img);
269 break;
271 case "gif":
272 $src = imagecreatefromgif($img);
273 break;
275 case "bmp":
276 $src = Helpers::imagecreatefrombmp($img);
277 break;
279 default:
280 return; // Unsupported image type
283 if ($src == null) {
284 return;
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);
294 if ($ti >= 0) {
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
303 if ($bg_x < 0) {
304 $dst_x = 0;
305 $src_x = -$bg_x;
306 } else {
307 $src_x = 0;
308 $dst_x = $bg_x;
311 if ($bg_y < 0) {
312 $dst_y = 0;
313 $src_y = -$bg_y;
314 } else {
315 $src_y = 0;
316 $dst_y = $bg_y;
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
321 $start_x = $bg_x;
322 $start_y = $bg_y;
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) {
333 if ($bg_x < 0) {
334 $dst_x = 0;
335 $src_x = -$bg_x;
336 $w = $img_w + $bg_x;
337 } else {
338 $dst_x = $bg_x;
339 $src_x = 0;
340 $w = $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) {
348 if ($bg_y < 0) {
349 $dst_y = 0;
350 $src_y = -$bg_y;
351 $h = $img_h + $bg_y;
352 } else {
353 $dst_y = $bg_y;
354 $src_y = 0;
355 $h = $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) {
366 if ($bg_x < 0) {
367 $dst_x = 0;
368 $src_x = -$bg_x;
369 $w = $img_w + $bg_x;
370 } else {
371 $dst_x = $bg_x;
372 $src_x = 0;
373 $w = $img_w;
376 if ($bg_y < 0) {
377 $dst_y = 0;
378 $src_y = -$bg_y;
379 $h = $img_h + $bg_y;
380 } else {
381 $dst_y = $bg_y;
382 $src_y = 0;
383 $h = $img_h;
385 imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
388 } else {
389 print 'Unknown repeat!';
392 imagedestroy($src);
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);
412 } else {
413 $tmp_dir = $this->_dompdf->get_option("temp_dir");
414 $tmp_name = tempnam($tmp_dir, "bg_dompdf_img_");
415 @unlink($tmp_name);
416 $tmp_file = "$tmp_name.png";
418 //debugpng
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);
423 imagedestroy($bg);
425 //debugpng
426 if ($this->_dompdf->get_option("debugPng")) print '[_background_image unlink ' . $tmp_file . ']';
428 if (!$this->_dompdf->get_option("debugKeepTemp")) {
429 unlink($tmp_file);
433 $this->_canvas->clipping_end();
436 protected function _get_dash_pattern($style, $width)
438 $pattern = array();
440 switch ($style) {
441 default:
442 /*case "solid":
443 case "double":
444 case "groove":
445 case "inset":
446 case "outset":
447 case "ridge":*/
448 case "none":
449 break;
451 case "dotted":
452 if ($width <= 1)
453 $pattern = array($width, $width * 2);
454 else
455 $pattern = array($width);
456 break;
458 case "dashed":
459 $pattern = array(3 * $width);
460 break;
463 return $pattern;
466 protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
468 return;
471 protected function _border_hidden($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
473 return;
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);
496 return;
499 list($top, $right, $bottom, $left) = $widths;
501 // All this polygon business is for beveled corners...
502 switch ($side) {
503 case "top":
504 $points = array($x, $y,
505 $x + $length, $y,
506 $x + $length - $right, $y + $top,
507 $x + $left, $y + $top);
508 $this->_canvas->polygon($points, $color, null, null, true);
509 break;
511 case "bottom":
512 $points = array($x, $y,
513 $x + $length, $y,
514 $x + $length - $right, $y - $bottom,
515 $x + $left, $y - $bottom);
516 $this->_canvas->polygon($points, $color, null, null, true);
517 break;
519 case "left":
520 $points = array($x, $y,
521 $x, $y + $length,
522 $x + $left, $y + $length - $bottom,
523 $x + $left, $y + $top);
524 $this->_canvas->polygon($points, $color, null, null, true);
525 break;
527 case "right":
528 $points = array($x, $y,
529 $x, $y + $length,
530 $x - $right, $y + $length - $bottom,
531 $x - $right, $y + $top);
532 $this->_canvas->polygon($points, $color, null, null, true);
533 break;
535 default:
536 return;
540 protected function _apply_ratio($side, $ratio, $top, $right, $bottom, $left, &$x, &$y, &$length, &$r1, &$r2)
542 switch ($side) {
544 case "top":
545 $r1 -= $left * $ratio;
546 $r2 -= $right * $ratio;
547 $x += $left * $ratio;
548 $y += $top * $ratio;
549 $length -= $left * $ratio + $right * $ratio;
550 break;
552 case "bottom":
553 $r1 -= $right * $ratio;
554 $r2 -= $left * $ratio;
555 $x += $left * $ratio;
556 $y -= $bottom * $ratio;
557 $length -= $left * $ratio + $right * $ratio;
558 break;
560 case "left":
561 $r1 -= $top * $ratio;
562 $r2 -= $bottom * $ratio;
563 $x += $left * $ratio;
564 $y += $top * $ratio;
565 $length -= $top * $ratio + $bottom * $ratio;
566 break;
568 case "right":
569 $r1 -= $bottom * $ratio;
570 $r2 -= $top * $ratio;
571 $x -= $right * $ratio;
572 $y += $top * $ratio;
573 $length -= $top * $ratio + $bottom * $ratio;
574 break;
576 default:
577 return;
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)
626 if (!is_numeric($c))
627 return $c;
629 return min(1, $c + 0.16);
632 protected function _shade($c)
634 if (!is_numeric($c))
635 return $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)
642 switch ($side) {
643 case "top":
644 case "left":
645 $shade = array_map(array($this, "_shade"), $color);
646 $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
647 break;
649 case "bottom":
650 case "right":
651 $tint = array_map(array($this, "_tint"), $color);
652 $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
653 break;
655 default:
656 return;
660 protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
662 switch ($side) {
663 case "top":
664 case "left":
665 $tint = array_map(array($this, "_tint"), $color);
666 $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
667 break;
669 case "bottom":
670 case "right":
671 $shade = array_map(array($this, "_shade"), $color);
672 $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
673 break;
675 default:
676 return;
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;
685 $width = $$side;
686 $pattern = $this->_get_dash_pattern($pattern_name, $width);
688 $half_width = $width / 2;
689 $r1 -= $half_width;
690 $r2 -= $half_width;
691 $adjust = $r1 / 80;
692 $length -= $width;
694 switch ($side) {
695 case "top":
696 $x += $half_width;
697 $y += $half_width;
699 if ($r1 > 0) {
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);
705 if ($r2 > 0) {
706 $this->_canvas->arc($x + $length - $r2, $y + $r2, $r2, $r2, 45 - $adjust, 90 + $adjust, $color, $width, $pattern);
708 break;
710 case "bottom":
711 $x += $half_width;
712 $y -= $half_width;
714 if ($r1 > 0) {
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);
720 if ($r2 > 0) {
721 $this->_canvas->arc($x + $length - $r2, $y - $r2, $r2, $r2, 270 - $adjust, 315 + $adjust, $color, $width, $pattern);
723 break;
725 case "left":
726 $y += $half_width;
727 $x += $half_width;
729 if ($r1 > 0) {
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);
735 if ($r2 > 0) {
736 $this->_canvas->arc($x + $r2, $y + $length - $r2, $r2, $r2, 180 - $adjust, 225 + $adjust, $color, $width, $pattern);
738 break;
740 case "right":
741 $y += $half_width;
742 $x -= $half_width;
744 if ($r1 > 0) {
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);
750 if ($r2 > 0) {
751 $this->_canvas->arc($x - $r2, $y + $length - $r2, $r2, $r2, 315 - $adjust, 360 + $adjust, $color, $width, $pattern);
753 break;
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);