inline_renderer.cls.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. /**
  9. * Renders inline frames
  10. *
  11. * @access private
  12. * @package dompdf
  13. */
  14. class Inline_Renderer extends Abstract_Renderer {
  15. //........................................................................
  16. function render(Frame $frame) {
  17. $style = $frame->get_style();
  18. if ( !$frame->get_first_child() )
  19. return; // No children, no service
  20. // Draw the left border if applicable
  21. $bp = $style->get_border_properties();
  22. $widths = array($style->length_in_pt($bp["top"]["width"]),
  23. $style->length_in_pt($bp["right"]["width"]),
  24. $style->length_in_pt($bp["bottom"]["width"]),
  25. $style->length_in_pt($bp["left"]["width"]));
  26. // Draw the background & border behind each child. To do this we need
  27. // to figure out just how much space each child takes:
  28. list($x, $y) = $frame->get_first_child()->get_position();
  29. $w = null;
  30. $h = 0;
  31. // $x += $widths[3];
  32. // $y += $widths[0];
  33. $this->_set_opacity( $frame->get_opacity( $style->opacity ) );
  34. $first_row = true;
  35. foreach ($frame->get_children() as $child) {
  36. list($child_x, $child_y, $child_w, $child_h) = $child->get_padding_box();
  37. if ( !is_null($w) && $child_x < $x + $w ) {
  38. //This branch seems to be supposed to being called on the first part
  39. //of an inline html element, and the part after the if clause for the
  40. //parts after a line break.
  41. //But because $w initially mostly is 0, and gets updated only on the next
  42. //round, this seem to be never executed and the common close always.
  43. // The next child is on another line. Draw the background &
  44. // borders on this line.
  45. // Background:
  46. if ( ($bg = $style->background_color) !== "transparent" )
  47. $this->_canvas->filled_rectangle( $x, $y, $w, $h, $bg);
  48. if ( ($url = $style->background_image) && $url !== "none" ) {
  49. $this->_background_image($url, $x, $y, $w, $h, $style);
  50. }
  51. // If this is the first row, draw the left border
  52. if ( $first_row ) {
  53. if ( $bp["left"]["style"] !== "none" && $bp["left"]["color"] !== "transparent" && $bp["left"]["width"] > 0 ) {
  54. $method = "_border_" . $bp["left"]["style"];
  55. $this->$method($x, $y, $h + $widths[0] + $widths[2], $bp["left"]["color"], $widths, "left");
  56. }
  57. $first_row = false;
  58. }
  59. // Draw the top & bottom borders
  60. if ( $bp["top"]["style"] !== "none" && $bp["top"]["color"] !== "transparent" && $bp["top"]["width"] > 0 ) {
  61. $method = "_border_" . $bp["top"]["style"];
  62. $this->$method($x, $y, $w + $widths[1] + $widths[3], $bp["top"]["color"], $widths, "top");
  63. }
  64. if ( $bp["bottom"]["style"] !== "none" && $bp["bottom"]["color"] !== "transparent" && $bp["bottom"]["width"] > 0 ) {
  65. $method = "_border_" . $bp["bottom"]["style"];
  66. $this->$method($x, $y + $h + $widths[0] + $widths[2], $w + $widths[1] + $widths[3], $bp["bottom"]["color"], $widths, "bottom");
  67. }
  68. // Handle anchors & links
  69. $link_node = null;
  70. if ( $frame->get_node()->nodeName === "a" ) {
  71. $link_node = $frame->get_node();
  72. }
  73. else if ( $frame->get_parent()->get_node()->nodeName === "a" ){
  74. $link_node = $frame->get_parent()->get_node();
  75. }
  76. if ( $link_node && $href = $link_node->getAttribute("href") ) {
  77. $this->_canvas->add_link($href, $x, $y, $w, $h);
  78. }
  79. $x = $child_x;
  80. $y = $child_y;
  81. $w = $child_w;
  82. $h = $child_h;
  83. continue;
  84. }
  85. if ( is_null($w) )
  86. $w = $child_w;
  87. else
  88. $w += $child_w;
  89. $h = max($h, $child_h);
  90. if (DEBUG_LAYOUT && DEBUG_LAYOUT_INLINE) {
  91. $this->_debug_layout($child->get_border_box(), "blue");
  92. if (DEBUG_LAYOUT_PADDINGBOX) {
  93. $this->_debug_layout($child->get_padding_box(), "blue", array(0.5, 0.5));
  94. }
  95. }
  96. }
  97. // Handle the last child
  98. if ( ($bg = $style->background_color) !== "transparent" )
  99. $this->_canvas->filled_rectangle( $x + $widths[3], $y + $widths[0], $w, $h, $bg);
  100. //On continuation lines (after line break) of inline elements, the style got copied.
  101. //But a non repeatable background image should not be repeated on the next line.
  102. //But removing the background image above has never an effect, and removing it below
  103. //removes it always, even on the initial line.
  104. //Need to handle it elsewhere, e.g. on certain ...clone()... usages.
  105. // Repeat not given: default is Style::__construct
  106. // ... && (!($repeat = $style->background_repeat) || $repeat === "repeat" ...
  107. //different position? $this->_background_image($url, $x, $y, $w, $h, $style);
  108. if ( ($url = $style->background_image) && $url !== "none" )
  109. $this->_background_image($url, $x + $widths[3], $y + $widths[0], $w, $h, $style);
  110. // Add the border widths
  111. $w += $widths[1] + $widths[3];
  112. $h += $widths[0] + $widths[2];
  113. // make sure the border and background start inside the left margin
  114. $left_margin = $style->length_in_pt($style->margin_left);
  115. $x += $left_margin;
  116. // If this is the first row, draw the left border too
  117. if ( $first_row && $bp["left"]["style"] !== "none" && $bp["left"]["color"] !== "transparent" && $widths[3] > 0 ) {
  118. $method = "_border_" . $bp["left"]["style"];
  119. $this->$method($x, $y, $h, $bp["left"]["color"], $widths, "left");
  120. }
  121. // Draw the top & bottom borders
  122. if ( $bp["top"]["style"] !== "none" && $bp["top"]["color"] !== "transparent" && $widths[0] > 0 ) {
  123. $method = "_border_" . $bp["top"]["style"];
  124. $this->$method($x, $y, $w, $bp["top"]["color"], $widths, "top");
  125. }
  126. if ( $bp["bottom"]["style"] !== "none" && $bp["bottom"]["color"] !== "transparent" && $widths[2] > 0 ) {
  127. $method = "_border_" . $bp["bottom"]["style"];
  128. $this->$method($x, $y + $h, $w, $bp["bottom"]["color"], $widths, "bottom");
  129. }
  130. // pre_var_dump(get_class($frame->get_next_sibling()));
  131. // $last_row = get_class($frame->get_next_sibling()) !== 'Inline_Frame_Decorator';
  132. // Draw the right border if this is the last row
  133. if ( $bp["right"]["style"] !== "none" && $bp["right"]["color"] !== "transparent" && $widths[1] > 0 ) {
  134. $method = "_border_" . $bp["right"]["style"];
  135. $this->$method($x + $w, $y, $h, $bp["right"]["color"], $widths, "right");
  136. }
  137. // Only two levels of links frames
  138. $link_node = null;
  139. if ( $frame->get_node()->nodeName === "a" ) {
  140. $link_node = $frame->get_node();
  141. if ( ($name = $link_node->getAttribute("name")) || ($name = $link_node->getAttribute("id")) ) {
  142. $this->_canvas->add_named_dest($name);
  143. }
  144. }
  145. if ( $frame->get_parent() && $frame->get_parent()->get_node()->nodeName === "a" ){
  146. $link_node = $frame->get_parent()->get_node();
  147. }
  148. // Handle anchors & links
  149. if ( $link_node ) {
  150. if ( $href = $link_node->getAttribute("href") )
  151. $this->_canvas->add_link($href, $x, $y, $w, $h);
  152. }
  153. }
  154. }