table_cell_renderer.cls.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 table cells
  10. *
  11. * @access private
  12. * @package dompdf
  13. */
  14. class Table_Cell_Renderer extends Block_Renderer {
  15. //........................................................................
  16. function render(Frame $frame) {
  17. $style = $frame->get_style();
  18. if ( trim($frame->get_node()->nodeValue) === "" && $style->empty_cells === "hide" ) {
  19. return;
  20. }
  21. $this->_set_opacity( $frame->get_opacity( $style->opacity ) );
  22. list($x, $y, $w, $h) = $frame->get_border_box();
  23. // Draw our background, border and content
  24. if ( ($bg = $style->background_color) !== "transparent" ) {
  25. $this->_canvas->filled_rectangle($x, $y, $w, $h, $bg);
  26. }
  27. if ( ($url = $style->background_image) && $url !== "none" ) {
  28. $this->_background_image($url, $x, $y, $w, $h, $style);
  29. }
  30. $table = Table_Frame_Decorator::find_parent_table($frame);
  31. if ( $table->get_style()->border_collapse !== "collapse" ) {
  32. $this->_render_border($frame);
  33. $this->_render_outline($frame);
  34. return;
  35. }
  36. // The collapsed case is slightly complicated...
  37. // @todo Add support for outlines here
  38. $cellmap = $table->get_cellmap();
  39. $cells = $cellmap->get_spanned_cells($frame);
  40. $num_rows = $cellmap->get_num_rows();
  41. $num_cols = $cellmap->get_num_cols();
  42. // Determine the top row spanned by this cell
  43. $i = $cells["rows"][0];
  44. $top_row = $cellmap->get_row($i);
  45. // Determine if this cell borders on the bottom of the table. If so,
  46. // then we draw its bottom border. Otherwise the next row down will
  47. // draw its top border instead.
  48. if (in_array( $num_rows - 1, $cells["rows"])) {
  49. $draw_bottom = true;
  50. $bottom_row = $cellmap->get_row($num_rows - 1);
  51. } else
  52. $draw_bottom = false;
  53. // Draw the horizontal borders
  54. foreach ( $cells["columns"] as $j ) {
  55. $bp = $cellmap->get_border_properties($i, $j);
  56. $y = $top_row["y"] - $bp["top"]["width"] / 2;
  57. $col = $cellmap->get_column($j);
  58. $x = $col["x"] - $bp["left"]["width"] / 2;
  59. $w = $col["used-width"] + ($bp["left"]["width"] + $bp["right"]["width"] ) / 2;
  60. if ( $bp["top"]["style"] !== "none" && $bp["top"]["width"] > 0 ) {
  61. $widths = array($bp["top"]["width"],
  62. $bp["right"]["width"],
  63. $bp["bottom"]["width"],
  64. $bp["left"]["width"]);
  65. $method = "_border_". $bp["top"]["style"];
  66. $this->$method($x, $y, $w, $bp["top"]["color"], $widths, "top", "square");
  67. }
  68. if ( $draw_bottom ) {
  69. $bp = $cellmap->get_border_properties($num_rows - 1, $j);
  70. if ( $bp["bottom"]["style"] === "none" || $bp["bottom"]["width"] <= 0 )
  71. continue;
  72. $y = $bottom_row["y"] + $bottom_row["height"] + $bp["bottom"]["width"] / 2;
  73. $widths = array($bp["top"]["width"],
  74. $bp["right"]["width"],
  75. $bp["bottom"]["width"],
  76. $bp["left"]["width"]);
  77. $method = "_border_". $bp["bottom"]["style"];
  78. $this->$method($x, $y, $w, $bp["bottom"]["color"], $widths, "bottom", "square");
  79. }
  80. }
  81. $j = $cells["columns"][0];
  82. $left_col = $cellmap->get_column($j);
  83. if (in_array($num_cols - 1, $cells["columns"])) {
  84. $draw_right = true;
  85. $right_col = $cellmap->get_column($num_cols - 1);
  86. } else
  87. $draw_right = false;
  88. // Draw the vertical borders
  89. foreach ( $cells["rows"] as $i ) {
  90. $bp = $cellmap->get_border_properties($i, $j);
  91. $x = $left_col["x"] - $bp["left"]["width"] / 2;
  92. $row = $cellmap->get_row($i);
  93. $y = $row["y"] - $bp["top"]["width"] / 2;
  94. $h = $row["height"] + ($bp["top"]["width"] + $bp["bottom"]["width"])/ 2;
  95. if ( $bp["left"]["style"] !== "none" && $bp["left"]["width"] > 0 ) {
  96. $widths = array($bp["top"]["width"],
  97. $bp["right"]["width"],
  98. $bp["bottom"]["width"],
  99. $bp["left"]["width"]);
  100. $method = "_border_" . $bp["left"]["style"];
  101. $this->$method($x, $y, $h, $bp["left"]["color"], $widths, "left", "square");
  102. }
  103. if ( $draw_right ) {
  104. $bp = $cellmap->get_border_properties($i, $num_cols - 1);
  105. if ( $bp["right"]["style"] === "none" || $bp["right"]["width"] <= 0 )
  106. continue;
  107. $x = $right_col["x"] + $right_col["used-width"] + $bp["right"]["width"] / 2;
  108. $widths = array($bp["top"]["width"],
  109. $bp["right"]["width"],
  110. $bp["bottom"]["width"],
  111. $bp["left"]["width"]);
  112. $method = "_border_" . $bp["right"]["style"];
  113. $this->$method($x, $y, $h, $bp["right"]["color"], $widths, "right", "square");
  114. }
  115. }
  116. }
  117. }