Blame view

dompdf/src/Renderer/Image.php 3.78 KB
670b6d6f8   tuan   2016/09/22 - Copy...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  <?php
  /**
   * @package dompdf
   * @link    http://dompdf.github.com/
   * @author  Benj Carson <benjcarson@digitaljunkies.ca>
   * @author  Fabien Ménager <fabien.menager@gmail.com>
   * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
   */
  namespace Dompdf\Renderer;
  
  use Dompdf\Frame;
  use Dompdf\Image\Cache;
  
  /**
   * Image renderer
   *
   * @access  private
   * @package dompdf
   */
  class Image extends Block
  {
  
      function render(Frame $frame)
      {
          // Render background & borders
          $style = $frame->get_style();
          $cb = $frame->get_containing_block();
          list($x, $y, $w, $h) = $frame->get_border_box();
  
          $this->_set_opacity($frame->get_opacity($style->opacity));
  
          list($tl, $tr, $br, $bl) = $style->get_computed_border_radius($w, $h);
  
          $has_border_radius = $tl + $tr + $br + $bl > 0;
  
          if ($has_border_radius) {
              $this->_canvas->clipping_roundrectangle($x, $y, $w, $h, $tl, $tr, $br, $bl);
          }
  
          if (($bg = $style->background_color) !== "transparent") {
              $this->_canvas->filled_rectangle($x, $y, $w, $h, $bg);
          }
  
          if (($url = $style->background_image) && $url !== "none") {
              $this->_background_image($url, $x, $y, $w, $h, $style);
          }
  
          if ($has_border_radius) {
              $this->_canvas->clipping_end();
          }
  
          $this->_render_border($frame);
          $this->_render_outline($frame);
  
          list($x, $y) = $frame->get_padding_box();
  
          $x += $style->length_in_pt($style->padding_left, $cb["w"]);
          $y += $style->length_in_pt($style->padding_top, $cb["h"]);
  
          $w = $style->length_in_pt($style->width, $cb["w"]);
          $h = $style->length_in_pt($style->height, $cb["h"]);
  
          if ($has_border_radius) {
              list($wt, $wr, $wb, $wl) = array(
                  $style->border_top_width,
                  $style->border_right_width,
                  $style->border_bottom_width,
                  $style->border_left_width,
              );
  
              // we have to get the "inner" radius
              if ($tl > 0) {
                  $tl -= ($wt + $wl) / 2;
              }
              if ($tr > 0) {
                  $tr -= ($wt + $wr) / 2;
              }
              if ($br > 0) {
                  $br -= ($wb + $wr) / 2;
              }
              if ($bl > 0) {
                  $bl -= ($wb + $wl) / 2;
              }
  
              $this->_canvas->clipping_roundrectangle($x, $y, $w, $h, $tl, $tr, $br, $bl);
          }
  
          $src = $frame->get_image_url();
          $alt = null;
  
          if (Cache::is_broken($src) &&
              $alt = $frame->get_node()->getAttribute("alt")
          ) {
              $font = $style->font_family;
              $size = $style->font_size;
              $spacing = $style->word_spacing;
              $this->_canvas->text($x, $y, $alt,
                  $font, $size,
                  $style->color, $spacing);
          } else {
              $this->_canvas->image($src, $x, $y, $w, $h, $style->image_resolution);
          }
  
          if ($has_border_radius) {
              $this->_canvas->clipping_end();
          }
  
          if ($msg = $frame->get_image_msg()) {
              $parts = preg_split("/\s*
  \s*/", $msg);
              $height = 10;
              $_y = $alt ? $y + $h - count($parts) * $height : $y;
  
              foreach ($parts as $i => $_part) {
                  $this->_canvas->text($x, $_y + $i * $height, $_part, "times", $height * 0.8, array(0.5, 0.5, 0.5));
              }
          }
  
          if ($this->_dompdf->get_option("debugLayout") && $this->_dompdf->get_option("debugLayoutBlocks")) {
              $this->_debug_layout($frame->get_border_box(), "blue");
              if ($this->_dompdf->get_option("debugLayoutPaddingBox")) {
                  $this->_debug_layout($frame->get_padding_box(), "blue", array(0.5, 0.5));
              }
          }
      }
  }