table_cell_renderer.cls.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: table_cell_renderer.cls.php,v $
  6. * Created on: 2004-06-09
  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: table_cell_renderer.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Renders table cells
  41. *
  42. * @access private
  43. * @package dompdf
  44. */
  45. class Table_Cell_Renderer extends Block_Renderer {
  46. //........................................................................
  47. function render(Frame $frame) {
  48. $style = $frame->get_style();
  49. list($x, $y, $w, $h) = $frame->get_padding_box();
  50. // Draw our background, border and content
  51. if ( ($bg = $style->background_color) !== "transparent" ) {
  52. list($x, $y, $w, $h) = $frame->get_padding_box();
  53. $this->_canvas->filled_rectangle( $x, $y, $w, $h, $style->background_color );
  54. }
  55. if ( ($url = $style->background_image) && $url !== "none" ) {
  56. $this->_background_image($url, $x, $y, $w, $h, $style);
  57. }
  58. if ( $style->border_collapse !== "collapse" ) {
  59. $this->_render_border($frame, "bevel");
  60. return;
  61. }
  62. // The collapsed case is slightly complicated...
  63. $cellmap = Table_Frame_Decorator::find_parent_table($frame)->get_cellmap();
  64. $cells = $cellmap->get_spanned_cells($frame);
  65. $num_rows = $cellmap->get_num_rows();
  66. $num_cols = $cellmap->get_num_cols();
  67. // Determine the top row spanned by this cell
  68. $i = $cells["rows"][0];
  69. $top_row = $cellmap->get_row($i);
  70. // Determine if this cell borders on the bottom of the table. If so,
  71. // then we draw its bottom border. Otherwise the next row down will
  72. // draw its top border instead.
  73. if (in_array( $num_rows - 1, $cells["rows"])) {
  74. $draw_bottom = true;
  75. $bottom_row = $cellmap->get_row($num_rows - 1);
  76. } else
  77. $draw_bottom = false;
  78. // Draw the horizontal borders
  79. foreach ( $cells["columns"] as $j ) {
  80. $bp = $cellmap->get_border_properties($i, $j);
  81. $y = $top_row["y"] - $bp["top"]["width"] / 2;
  82. $col = $cellmap->get_column($j);
  83. $x = $col["x"] - $bp["left"]["width"] / 2;
  84. $w = $col["used-width"] + ($bp["left"]["width"] + $bp["right"]["width"] ) / 2;
  85. if ( $bp["top"]["style"] !== "none" && $bp["top"]["width"] > 0 ) {
  86. $widths = array($bp["top"]["width"],
  87. $bp["right"]["width"],
  88. $bp["bottom"]["width"],
  89. $bp["left"]["width"]);
  90. $method = "_border_". $bp["top"]["style"];
  91. $this->$method($x, $y, $w, $bp["top"]["color"], $widths, "top", "square");
  92. }
  93. if ( $draw_bottom ) {
  94. $bp = $cellmap->get_border_properties($num_rows - 1, $j);
  95. if ( $bp["bottom"]["style"] === "none" || $bp["bottom"]["width"] <= 0 )
  96. continue;
  97. $y = $bottom_row["y"] + $bottom_row["height"] + $bp["bottom"]["width"] / 2;
  98. $widths = array($bp["top"]["width"],
  99. $bp["right"]["width"],
  100. $bp["bottom"]["width"],
  101. $bp["left"]["width"]);
  102. $method = "_border_". $bp["bottom"]["style"];
  103. $this->$method($x, $y, $w, $bp["bottom"]["color"], $widths, "bottom", "square");
  104. }
  105. }
  106. $j = $cells["columns"][0];
  107. $left_col = $cellmap->get_column($j);
  108. if (in_array($num_cols - 1, $cells["columns"])) {
  109. $draw_right = true;
  110. $right_col = $cellmap->get_column($num_cols - 1);
  111. } else
  112. $draw_right = false;
  113. // Draw the vertical borders
  114. foreach ( $cells["rows"] as $i ) {
  115. $bp = $cellmap->get_border_properties($i, $j);
  116. $x = $left_col["x"] - $bp["left"]["width"] / 2;
  117. $row = $cellmap->get_row($i);
  118. $y = $row["y"] - $bp["top"]["width"] / 2;
  119. $h = $row["height"] + ($bp["top"]["width"] + $bp["bottom"]["width"])/ 2;
  120. if ( $bp["left"]["style"] !== "none" && $bp["left"]["width"] > 0 ) {
  121. $widths = array($bp["top"]["width"],
  122. $bp["right"]["width"],
  123. $bp["bottom"]["width"],
  124. $bp["left"]["width"]);
  125. $method = "_border_" . $bp["left"]["style"];
  126. $this->$method($x, $y, $h, $bp["left"]["color"], $widths, "left", "square");
  127. }
  128. if ( $draw_right ) {
  129. $bp = $cellmap->get_border_properties($i, $num_cols - 1);
  130. if ( $bp["right"]["style"] === "none" || $bp["right"]["width"] <= 0 )
  131. continue;
  132. $x = $right_col["x"] + $right_col["used-width"] + $bp["right"]["width"] / 2;
  133. $widths = array($bp["top"]["width"],
  134. $bp["right"]["width"],
  135. $bp["bottom"]["width"],
  136. $bp["left"]["width"]);
  137. $method = "_border_" . $bp["right"]["style"];
  138. $this->$method($x, $y, $h, $bp["right"]["color"], $widths, "right", "square");
  139. }
  140. }
  141. }
  142. }