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
12 use Dompdf\FrameDecorator\Table
as TableFrameDecorator
;
13 use Dompdf\FrameDecorator\TableCell
as TableCellFrameDecorator
;
16 * Maps table cells to the table grid.
18 * This class resolves borders in tables with collapsed borders and helps
19 * place row & column spanned table cells.
26 * Border style weight lookup for collapsed border resolution.
30 protected static $_BORDER_STYLE_SCORE = array(
44 * The table object this cellmap is attached to.
46 * @var TableFrameDecorator
51 * The total number of rows in the table
58 * The total number of columns in the table
65 * 2D array mapping <row,column> to frames
72 * 1D array of column dimensions
79 * 1D array of row dimensions
86 * 2D array of border specs
93 * 1D Array mapping frames to (multiple) <row, col> pairs, keyed on frame_id.
100 * Current column when adding cells, 0-based
107 * Current row when adding cells, 0-based
114 * Tells wether the columns' width can be modified
118 private $_columns_locked = false;
121 * Tells wether the table has table-layout:fixed
125 private $_fixed_layout = false;
128 * @param TableFrameDecorator $table
130 public function __construct(TableFrameDecorator
$table)
132 $this->_table
= $table;
139 public function reset()
141 $this->_num_rows
= 0;
142 $this->_num_cols
= 0;
144 $this->_cells
= array();
145 $this->_frames
= array();
147 if (!$this->_columns_locked
) {
148 $this->_columns
= array();
151 $this->_rows
= array();
153 $this->_borders
= array();
155 $this->__col
= $this->__row
= 0;
161 public function lock_columns()
163 $this->_columns_locked
= true;
169 public function is_columns_locked()
171 return $this->_columns_locked
;
177 public function set_layout_fixed($fixed)
179 $this->_fixed_layout
= $fixed;
185 public function is_layout_fixed()
187 return $this->_fixed_layout
;
193 public function get_num_rows()
195 return $this->_num_rows
;
201 public function get_num_cols()
203 return $this->_num_cols
;
209 public function &get_columns()
211 return $this->_columns
;
217 public function set_columns($columns)
219 $this->_columns
= $columns;
227 public function &get_column($i)
229 if (!isset($this->_columns
[$i])) {
230 $this->_columns
[$i] = array(
234 "used-width" => null,
241 return $this->_columns
[$i];
247 public function &get_rows()
257 public function &get_row($j)
259 if (!isset($this->_rows
[$j])) {
260 $this->_rows
[$j] = array(
267 return $this->_rows
[$j];
274 * @param null|mixed $prop
278 public function get_border($i, $j, $h_v, $prop = null)
280 if (!isset($this->_borders
[$i][$j][$h_v])) {
281 $this->_borders
[$i][$j][$h_v] = array(
289 return $this->_borders
[$i][$j][$h_v][$prop];
292 return $this->_borders
[$i][$j][$h_v];
301 public function get_border_properties($i, $j)
304 "top" => $this->get_border($i, $j, "horizontal"),
305 "right" => $this->get_border($i, $j +
1, "vertical"),
306 "bottom" => $this->get_border($i +
1, $j, "horizontal"),
307 "left" => $this->get_border($i, $j, "vertical"),
312 * @param Frame $frame
316 public function get_spanned_cells(Frame
$frame)
318 $key = $frame->get_id();
320 if (isset($this->_frames
[$key])) {
321 return $this->_frames
[$key];
328 * @param Frame $frame
332 public function frame_exists_in_cellmap(Frame
$frame)
334 $key = $frame->get_id();
336 return isset($this->_frames
[$key]);
340 * @param Frame $frame
345 public function get_frame_position(Frame
$frame)
347 global $_dompdf_warnings;
349 $key = $frame->get_id();
351 if (!isset($this->_frames
[$key])) {
352 throw new Exception("Frame not found in cellmap");
355 $col = $this->_frames
[$key]["columns"][0];
356 $row = $this->_frames
[$key]["rows"][0];
358 if (!isset($this->_columns
[$col])) {
359 $_dompdf_warnings[] = "Frame not found in columns array. Check your table layout for missing or extra TDs.";
362 $x = $this->_columns
[$col]["x"];
365 if (!isset($this->_rows
[$row])) {
366 $_dompdf_warnings[] = "Frame not found in row array. Check your table layout for missing or extra TDs.";
369 $y = $this->_rows
[$row]["y"];
372 return array($x, $y, "x" => $x, "y" => $y);
376 * @param Frame $frame
381 public function get_frame_width(Frame
$frame)
383 $key = $frame->get_id();
385 if (!isset($this->_frames
[$key])) {
386 throw new Exception("Frame not found in cellmap");
389 $cols = $this->_frames
[$key]["columns"];
391 foreach ($cols as $i) {
392 $w +
= $this->_columns
[$i]["used-width"];
399 * @param Frame $frame
405 public function get_frame_height(Frame
$frame)
407 $key = $frame->get_id();
409 if (!isset($this->_frames
[$key])) {
410 throw new Exception("Frame not found in cellmap");
413 $rows = $this->_frames
[$key]["rows"];
415 foreach ($rows as $i) {
416 if (!isset($this->_rows
[$i])) {
417 throw new Exception("The row #$i could not be found, please file an issue in the tracker with the HTML code");
420 $h +
= $this->_rows
[$i]["height"];
428 * @param mixed $width
430 public function set_column_width($j, $width)
432 if ($this->_columns_locked
) {
436 $col =& $this->get_column($j);
437 $col["used-width"] = $width;
438 $next_col =& $this->get_column($j +
1);
439 $next_col["x"] = $next_col["x"] +
$width;
444 * @param mixed $height
446 public function set_row_height($i, $height)
448 $row =& $this->get_row($i);
450 if ($row["height"] !== null && $height <= $row["height"]) {
454 $row["height"] = $height;
455 $next_row =& $this->get_row($i +
1);
456 $next_row["y"] = $row["y"] +
$height;
464 * @param mixed $border_spec
468 protected function _resolve_border($i, $j, $h_v, $border_spec)
470 $n_width = $border_spec["width"];
471 $n_style = $border_spec["style"];
473 if (!isset($this->_borders
[$i][$j][$h_v])) {
474 $this->_borders
[$i][$j][$h_v] = $border_spec;
476 return $this->_borders
[$i][$j][$h_v]["width"];
479 $border = & $this->_borders
[$i][$j][$h_v];
481 $o_width = $border["width"];
482 $o_style = $border["style"];
484 if (($n_style === "hidden" ||
485 $n_width > $o_width ||
490 ($o_width == $n_width &&
491 in_array($n_style, self
::$_BORDER_STYLE_SCORE) &&
492 self
::$_BORDER_STYLE_SCORE[$n_style] > self
::$_BORDER_STYLE_SCORE[$o_style])
494 $border = $border_spec;
497 return $border["width"];
501 * @param Frame $frame
503 public function add_frame(Frame
$frame)
505 $style = $frame->get_style();
506 $display = $style->display
;
508 $collapse = $this->_table
->get_style()->border_collapse
== "collapse";
510 // Recursively add the frames within tables, table-row-groups and table-rows
511 if ($display === "table-row" ||
512 $display === "table" ||
513 $display === "inline-table" ||
514 in_array($display, TableFrameDecorator
::$ROW_GROUPS)
517 $start_row = $this->__row
;
518 foreach ($frame->get_children() as $child) {
519 // Ignore all Text frames and :before/:after pseudo-selector elements.
520 if (!($child instanceof FrameDecorator\Text
) && $child->get_node()->nodeName
!== 'dompdf_generated') {
521 $this->add_frame($child);
525 if ($display === "table-row") {
529 $num_rows = $this->__row
- $start_row - 1;
530 $key = $frame->get_id();
532 // Row groups always span across the entire table
533 $this->_frames
[$key]["columns"] = range(0, max(0, $this->_num_cols
- 1));
534 $this->_frames
[$key]["rows"] = range($start_row, max(0, $this->__row
- 1));
535 $this->_frames
[$key]["frame"] = $frame;
537 if ($display !== "table-row" && $collapse) {
539 $bp = $style->get_border_properties();
541 // Resolve the borders
542 for ($i = 0; $i < $num_rows +
1; $i++
) {
543 $this->_resolve_border($start_row +
$i, 0, "vertical", $bp["left"]);
544 $this->_resolve_border($start_row +
$i, $this->_num_cols
, "vertical", $bp["right"]);
547 for ($j = 0; $j < $this->_num_cols
; $j++
) {
548 $this->_resolve_border($start_row, $j, "horizontal", $bp["top"]);
549 $this->_resolve_border($this->__row
, $j, "horizontal", $bp["bottom"]);
555 $node = $frame->get_node();
557 // Determine where this cell is going
558 $colspan = $node->getAttribute("colspan");
559 $rowspan = $node->getAttribute("rowspan");
563 $node->setAttribute("colspan", 1);
568 $node->setAttribute("rowspan", 1);
570 $key = $frame->get_id();
572 $bp = $style->get_border_properties();
575 // Add the frame to the cellmap
576 $max_left = $max_right = 0;
578 // Find the next available column (fix by Ciro Mondueri)
580 while (isset($this->_cells
[$this->__row
][$ac])) {
587 for ($i = 0; $i < $rowspan; $i++
) {
588 $row = $this->__row +
$i;
590 $this->_frames
[$key]["rows"][] = $row;
592 for ($j = 0; $j < $colspan; $j++
) {
593 $this->_cells
[$row][$this->__col +
$j] = $frame;
597 // Resolve vertical borders
598 $max_left = max($max_left, $this->_resolve_border($row, $this->__col
, "vertical", $bp["left"]));
599 $max_right = max($max_right, $this->_resolve_border($row, $this->__col +
$colspan, "vertical", $bp["right"]));
603 $max_top = $max_bottom = 0;
606 for ($j = 0; $j < $colspan; $j++
) {
607 $col = $this->__col +
$j;
608 $this->_frames
[$key]["columns"][] = $col;
611 // Resolve horizontal borders
612 $max_top = max($max_top, $this->_resolve_border($this->__row
, $col, "horizontal", $bp["top"]));
613 $max_bottom = max($max_bottom, $this->_resolve_border($this->__row +
$rowspan, $col, "horizontal", $bp["bottom"]));
617 $this->_frames
[$key]["frame"] = $frame;
619 // Handle seperated border model
621 list($h, $v) = $this->_table
->get_style()->border_spacing
;
623 // Border spacing is effectively a margin between cells
624 $v = $style->length_in_pt($v) / 2;
625 $h = $style->length_in_pt($h) / 2;
626 $style->margin
= "$v $h";
628 // The additional 1/2 width gets added to the table proper
630 // Drop the frame's actual border
631 $style->border_left_width
= $max_left / 2;
632 $style->border_right_width
= $max_right / 2;
633 $style->border_top_width
= $max_top / 2;
634 $style->border_bottom_width
= $max_bottom / 2;
635 $style->margin
= "none";
638 if (!$this->_columns_locked
) {
639 // Resolve the frame's width
640 if ($this->_fixed_layout
) {
641 list($frame_min, $frame_max) = array(0, 10e-10);
643 list($frame_min, $frame_max) = $frame->get_min_max_width();
646 $width = $style->width
;
649 if (Helpers
::is_percent($width)) {
651 $val = (float)rtrim($width, "% ") / $colspan;
652 } else if ($width !== "auto") {
654 $val = $style->length_in_pt($frame_min) / $colspan;
659 for ($cs = 0; $cs < $colspan; $cs++
) {
661 // Resolve the frame's width(s) with other cells
662 $col =& $this->get_column($this->__col +
$cs);
664 // Note: $var is either 'percent' or 'absolute'. We compare the
665 // requested percentage or absolute values with the existing widths
666 // and adjust accordingly.
667 if (isset($var) && $val > $col[$var]) {
669 $col["auto"] = false;
672 $min +
= $col["min-width"];
673 $max +
= $col["max-width"];
676 if ($frame_min > $min) {
677 // The frame needs more space. Expand each sub-column
678 // FIXME try to avoid putting this dummy value when table-layout:fixed
679 $inc = ($this->is_layout_fixed() ?
10e-10 : ($frame_min - $min) / $colspan);
680 for ($c = 0; $c < $colspan; $c++
) {
681 $col =& $this->get_column($this->__col +
$c);
682 $col["min-width"] +
= $inc;
686 if ($frame_max > $max) {
687 // FIXME try to avoid putting this dummy value when table-layout:fixed
688 $inc = ($this->is_layout_fixed() ?
10e-10 : ($frame_max - $max) / $colspan);
689 for ($c = 0; $c < $colspan; $c++
) {
690 $col =& $this->get_column($this->__col +
$c);
691 $col["max-width"] +
= $inc;
696 $this->__col +
= $colspan;
697 if ($this->__col
> $this->_num_cols
) {
698 $this->_num_cols
= $this->__col
;
705 public function add_row()
710 // Find the next available column
712 while (isset($this->_cells
[$this->__row
][$i])) {
720 * Remove a row from the cellmap.
724 public function remove_row(Frame
$row)
726 $key = $row->get_id();
727 if (!isset($this->_frames
[$key])) {
728 return; // Presumably this row has alredy been removed
731 $this->__row
= $this->_num_rows
--;
733 $rows = $this->_frames
[$key]["rows"];
734 $columns = $this->_frames
[$key]["columns"];
736 // Remove all frames from this row
737 foreach ($rows as $r) {
738 foreach ($columns as $c) {
739 if (isset($this->_cells
[$r][$c])) {
740 $id = $this->_cells
[$r][$c]->get_id();
742 $this->_cells
[$r][$c] = null;
743 unset($this->_cells
[$r][$c]);
745 // has multiple rows?
746 if (isset($this->_frames
[$id]) && count($this->_frames
[$id]["rows"]) > 1) {
747 // remove just the desired row, but leave the frame
748 if (($row_key = array_search($r, $this->_frames
[$id]["rows"])) !== false) {
749 unset($this->_frames
[$id]["rows"][$row_key]);
754 $this->_frames
[$id] = null;
755 unset($this->_frames
[$id]);
759 $this->_rows
[$r] = null;
760 unset($this->_rows
[$r]);
763 $this->_frames
[$key] = null;
764 unset($this->_frames
[$key]);
768 * Remove a row group from the cellmap.
770 * @param Frame $group The group to remove
772 public function remove_row_group(Frame
$group)
774 $key = $group->get_id();
775 if (!isset($this->_frames
[$key])) {
776 return; // Presumably this row has alredy been removed
779 $iter = $group->get_first_child();
781 $this->remove_row($iter);
782 $iter = $iter->get_next_sibling();
785 $this->_frames
[$key] = null;
786 unset($this->_frames
[$key]);
790 * Update a row group after rows have been removed
792 * @param Frame $group The group to update
793 * @param Frame $last_row The last row in the row group
795 public function update_row_group(Frame
$group, Frame
$last_row)
797 $g_key = $group->get_id();
798 $r_key = $last_row->get_id();
800 $r_rows = $this->_frames
[$r_key]["rows"];
801 $this->_frames
[$g_key]["rows"] = range($this->_frames
[$g_key]["rows"][0], end($r_rows));
807 public function assign_x_positions()
809 // Pre-condition: widths must be resolved and assigned to columns and
810 // column[0]["x"] must be set.
812 if ($this->_columns_locked
) {
816 $x = $this->_columns
[0]["x"];
817 foreach (array_keys($this->_columns
) as $j) {
818 $this->_columns
[$j]["x"] = $x;
819 $x +
= $this->_columns
[$j]["used-width"];
826 public function assign_frame_heights()
828 // Pre-condition: widths and heights of each column & row must be
830 foreach ($this->_frames
as $arr) {
831 $frame = $arr["frame"];
834 foreach ($arr["rows"] as $row) {
835 if (!isset($this->_rows
[$row])) {
836 // The row has been removed because of a page split, so skip it.
840 $h +
= $this->_rows
[$row]["height"];
843 if ($frame instanceof TableCellFrameDecorator
) {
844 $frame->set_cell_height($h);
846 $frame->get_style()->height
= $h;
852 * Re-adjust frame height if the table height is larger than its content
854 public function set_frame_heights($table_height, $content_height)
856 // Distribute the increased height proportionally amongst each row
857 foreach ($this->_frames
as $arr) {
858 $frame = $arr["frame"];
861 foreach ($arr["rows"] as $row) {
862 if (!isset($this->_rows
[$row])) {
866 $h +
= $this->_rows
[$row]["height"];
869 if ($content_height > 0) {
870 $new_height = ($h / $content_height) * $table_height;
875 if ($frame instanceof TableCellFrameDecorator
) {
876 $frame->set_cell_height($new_height);
878 $frame->get_style()->height
= $new_height;
884 * Used for debugging:
888 public function __toString()
891 $str .= "Columns:<br/>";
892 $str .= Helpers
::pre_r($this->_columns
, true);
893 $str .= "Rows:<br/>";
894 $str .= Helpers
::pre_r($this->_rows
, true);
896 $str .= "Frames:<br/>";
898 foreach ($this->_frames
as $key => $val) {
899 $arr[$key] = array("columns" => $val["columns"], "rows" => $val["rows"]);
902 $str .= Helpers
::pre_r($arr, true);
904 if (php_sapi_name() == "cli") {
905 $str = strip_tags(str_replace(array("<br/>", "<b>", "</b>"),
906 array("\n", chr(27) . "[01;33m", chr(27) . "[0m"),