inline_renderer.cls.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: inline_renderer.cls.php,v $
  6. * Created on: 2004-06-30
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @package dompdf
  37. */
  38. /* $Id: inline_renderer.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Renders inline frames
  41. *
  42. * @access private
  43. * @package dompdf
  44. */
  45. class Inline_Renderer extends Abstract_Renderer {
  46. //........................................................................
  47. function render(Frame $frame) {
  48. $style = $frame->get_style();
  49. if ( !$frame->get_first_child() )
  50. return; // No children, no service
  51. // Draw the left border if applicable
  52. $bp = $style->get_border_properties();
  53. $widths = array($style->length_in_pt($bp["top"]["width"]),
  54. $style->length_in_pt($bp["right"]["width"]),
  55. $style->length_in_pt($bp["bottom"]["width"]),
  56. $style->length_in_pt($bp["left"]["width"]));
  57. // Draw the background & border behind each child. To do this we need
  58. // to figure out just how much space each child takes:
  59. list($x, $y) = $frame->get_first_child()->get_position();
  60. $w = null;
  61. $h = 0;
  62. // $x += $widths[3];
  63. // $y += $widths[0];
  64. $first_row = true;
  65. foreach ($frame->get_children() as $child) {
  66. list($child_x, $child_y, $child_w, $child_h) = $child->get_padding_box();
  67. $child_h += $widths[2];
  68. if ( !is_null($w) && $child_x < $x + $w ) {
  69. //This branch seems to be supposed to being called on the first part
  70. //of an inline html element, and the part after the if clause for the
  71. //parts after a line break.
  72. //But because $w initially mostly is 0, and gets updated only on the next
  73. //round, this seem to be never executed and the common close always.
  74. // The next child is on another line. Draw the background &
  75. // borders on this line.
  76. // Background:
  77. if ( ($bg = $style->background_color) !== "transparent" )
  78. $this->_canvas->filled_rectangle( $x, $y, $w, $h, $style->background_color);
  79. if ( ($url = $style->background_image) && $url !== "none" ) {
  80. $this->_background_image($url, $x, $y, $w, $h, $style);
  81. }
  82. // If this is the first row, draw the left border
  83. if ( $first_row ) {
  84. if ( $bp["left"]["style"] !== "none" && $bp["left"]["width"] > 0 ) {
  85. $method = "_border_" . $bp["left"]["style"];
  86. $this->$method($x, $y, $h + $widths[0] + $widths[2], $bp["left"]["color"], $widths, "left");
  87. }
  88. $first_row = false;
  89. }
  90. // Draw the top & bottom borders
  91. if ( $bp["top"]["style"] !== "none" && $bp["top"]["width"] > 0 ) {
  92. $method = "_border_" . $bp["top"]["style"];
  93. $this->$method($x, $y, $w + $widths[1] + $widths[3], $bp["top"]["color"], $widths, "top");
  94. }
  95. if ( $bp["bottom"]["style"] !== "none" && $bp["bottom"]["width"] > 0 ) {
  96. $method = "_border_" . $bp["bottom"]["style"];
  97. $this->$method($x, $y + $h + $widths[0] + $widths[2], $w + $widths[1] + $widths[3], $bp["bottom"]["color"], $widths, "bottom");
  98. }
  99. // Handle anchors & links
  100. if ( $frame->get_node()->nodeName === "a" ) {
  101. if ( $href = $frame->get_node()->getAttribute("href") )
  102. $this->_canvas->add_link($href, $x, $y, $w, $h);
  103. }
  104. $x = $child_x;
  105. $y = $child_y;
  106. $w = $child_w;
  107. $h = $child_h;
  108. continue;
  109. }
  110. if ( is_null($w) )
  111. $w = $child_w;
  112. else
  113. $w += $child_w;
  114. $h = max($h, $child_h);
  115. }
  116. // Handle the last child
  117. if ( ($bg = $style->background_color) !== "transparent" )
  118. $this->_canvas->filled_rectangle( $x + $widths[3], $y + $widths[0], $w, $h, $style->background_color);
  119. //On continuation lines (after line break) of inline elements, the style got copied.
  120. //But a non repeatable background image should not be repeated on the next line.
  121. //But removing the background image above has never an effect, and removing it below
  122. //removes it always, even on the initial line.
  123. //Need to handle it elsewhere, e.g. on certain ...clone()... usages.
  124. // Repeat not given: default is Style::__construct
  125. // ... && (!($repeat = $style->background_repeat) || $repeat === "repeat" ...
  126. //different position? $this->_background_image($url, $x, $y, $w, $h, $style);
  127. if ( ($url = $style->background_image) && $url !== "none" )
  128. $this->_background_image($url, $x + $widths[3], $y + $widths[0], $w, $h, $style);
  129. // Add the border widths
  130. $w += $widths[1] + $widths[3];
  131. $h += $widths[0] + $widths[2];
  132. // make sure the border and background start inside the left margin
  133. $left_margin = $style->length_in_pt($style->margin_left);
  134. $x += $left_margin;
  135. // If this is the first row, draw the left border too
  136. if ( $first_row && $bp["left"]["style"] !== "none" && $widths[3] > 0 ) {
  137. $method = "_border_" . $bp["left"]["style"];
  138. $this->$method($x, $y, $h, $bp["left"]["color"], $widths, "left");
  139. }
  140. // Draw the top & bottom borders
  141. if ( $bp["top"]["style"] !== "none" && $widths[0] > 0 ) {
  142. $method = "_border_" . $bp["top"]["style"];
  143. $this->$method($x, $y, $w, $bp["top"]["color"], $widths, "top");
  144. }
  145. if ( $bp["bottom"]["style"] !== "none" && $widths[2] > 0 ) {
  146. $method = "_border_" . $bp["bottom"]["style"];
  147. $this->$method($x, $y + $h, $w, $bp["bottom"]["color"], $widths, "bottom");
  148. }
  149. // pre_var_dump(get_class($frame->get_next_sibling()));
  150. // $last_row = get_class($frame->get_next_sibling()) !== 'Inline_Frame_Decorator';
  151. // Draw the right border if this is the last row
  152. if ( $bp["right"]["style"] !== "none" && $widths[1] > 0 ) {
  153. $method = "_border_" . $bp["right"]["style"];
  154. $this->$method($x + $w, $y, $h, $bp["right"]["color"], $widths, "right");
  155. }
  156. // Handle anchors & links
  157. if ( $frame->get_node()->nodeName === "a" ) {
  158. if ( $name = $frame->get_node()->getAttribute("name") )
  159. $this->_canvas->add_named_dest($name);
  160. if ( $href = $frame->get_node()->getAttribute("href") )
  161. $this->_canvas->add_link($href, $x, $y, $w, $h);
  162. }
  163. }
  164. }