PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Renderer/Inline.php

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