cellmap.cls.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: cellmap.cls.php,v $
  6. * Created on: 2004-07-28
  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: cellmap.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Maps table cells to the table grid.
  41. *
  42. * This class resolves borders in tables with collapsed borders and helps
  43. * place row & column spanned table cells.
  44. *
  45. * @access private
  46. * @package dompdf
  47. */
  48. class Cellmap {
  49. /**
  50. * Border style weight lookup for collapsed border resolution.
  51. *
  52. * @var array
  53. */
  54. static protected $_BORDER_STYLE_SCORE = array("inset" => 1,
  55. "groove" => 2,
  56. "outset" => 3,
  57. "ridge" => 4,
  58. "dotted" => 5,
  59. "dashed" => 6,
  60. "solid" => 7,
  61. "double" => 8,
  62. "none" => 0);
  63. /**
  64. * The table object this cellmap is attached to.
  65. *
  66. * @var Table_Frame_Decorator
  67. */
  68. protected $_table;
  69. /**
  70. * The total number of rows in the table
  71. *
  72. * @var int
  73. */
  74. protected $_num_rows;
  75. /**
  76. * The total number of columns in the table
  77. *
  78. * @var int
  79. */
  80. protected $_num_cols;
  81. /**
  82. * 2D array mapping <row,column> to frames
  83. *
  84. * @var array
  85. */
  86. protected $_cells;
  87. /**
  88. * 1D array of column dimensions
  89. *
  90. * @var array
  91. */
  92. protected $_columns;
  93. /**
  94. * 1D array of row dimensions
  95. *
  96. * @var array
  97. */
  98. protected $_rows;
  99. /**
  100. * 2D array of border specs
  101. *
  102. * @var array
  103. */
  104. protected $_borders;
  105. /**
  106. * 1D Array mapping frames to (multiple) <row, col> pairs, keyed on
  107. * frame_id.
  108. *
  109. * @var array
  110. */
  111. protected $_frames;
  112. /**
  113. * Current column when adding cells, 0-based
  114. *
  115. * @var int
  116. */
  117. private $__col;
  118. /**
  119. * Current row when adding cells, 0-based
  120. *
  121. * @var int
  122. */
  123. private $__row;
  124. //........................................................................
  125. function __construct(Table_Frame_Decorator $table) {
  126. $this->_table = $table;
  127. $this->reset();
  128. }
  129. //........................................................................
  130. function reset() {
  131. $this->_num_rows = 0;
  132. $this->_num_cols = 0;
  133. $this->_cells = array();
  134. $this->_frames = array();
  135. $this->_columns = array();
  136. $this->_rows = array();
  137. $this->_borders = array();
  138. $this->__col = $this->__row = 0;
  139. }
  140. //........................................................................
  141. function get_num_rows() { return $this->_num_rows; }
  142. function get_num_cols() { return $this->_num_cols; }
  143. function &get_columns() {
  144. return $this->_columns;
  145. }
  146. function &get_column($i) {
  147. if ( !isset($this->_columns[$i]) )
  148. $this->_columns[$i] = array("x" => 0,
  149. "min-width" => 0,
  150. "max-width" => 0,
  151. "used-width" => null,
  152. "absolute" => 0,
  153. "percent" => 0,
  154. "auto" => true);
  155. return $this->_columns[$i];
  156. }
  157. function &get_rows() {
  158. return $this->_rows;
  159. }
  160. function &get_row($j) {
  161. if ( !isset($this->_rows[$j]) )
  162. $this->_rows[$j] = array("y" => 0,
  163. "first-column" => 0,
  164. "height" => null);
  165. return $this->_rows[$j];
  166. }
  167. function get_border($i, $j, $h_v, $prop = null) {
  168. if ( !isset($this->_borders[$i][$j][$h_v]) )
  169. $this->_borders[$i][$j][$h_v] = array("width" => 0,
  170. "style" => "solid",
  171. "color" => "black");
  172. if ( isset($prop) )
  173. return $this->_borders[$i][$j][$h_v][$prop];
  174. return $this->_borders[$i][$j][$h_v];
  175. }
  176. function get_border_properties($i, $j) {
  177. $left = $this->get_border($i, $j, "vertical");
  178. $right = $this->get_border($i, $j+1, "vertical");
  179. $top = $this->get_border($i, $j, "horizontal");
  180. $bottom = $this->get_border($i+1, $j, "horizontal");
  181. return compact("top", "bottom", "left", "right");
  182. }
  183. //........................................................................
  184. function get_spanned_cells($frame) {
  185. $key = $frame->get_id();
  186. if ( !isset($this->_frames[$key]) ) {
  187. throw new DOMPDF_Internal_Exception("Frame not found in cellmap");
  188. }
  189. return $this->_frames[$key];
  190. }
  191. function frame_exists_in_cellmap($frame) {
  192. $key = $frame->get_id();
  193. return isset($this->_frames[$key]);
  194. }
  195. function get_frame_position($frame) {
  196. global $_dompdf_warnings;
  197. $key = $frame->get_id();
  198. if ( !isset($this->_frames[$key]) ) {
  199. throw new DOMPDF_Internal_Exception("Frame not found in cellmap");
  200. }
  201. $col = $this->_frames[$key]["columns"][0];
  202. $row = $this->_frames[$key]["rows"][0];
  203. if ( !isset($this->_columns[$col])) {
  204. $_dompdf_warnings[] = "Frame not found in columns array. Check your table layout for missing or extra TDs.";
  205. $x = 0;
  206. } else
  207. $x = $this->_columns[$col]["x"];
  208. if ( !isset($this->_rows[$row])) {
  209. $_dompdf_warnings[] = "Frame not found in row array. Check your table layout for missing or extra TDs.";
  210. $y = 0;
  211. } else
  212. $y = $this->_rows[$row]["y"];
  213. return array($x, $y, "x" => $x, "y" => $y);
  214. }
  215. function get_frame_width($frame) {
  216. $key = $frame->get_id();
  217. if ( !isset($this->_frames[$key]) ) {
  218. throw new DOMPDF_Internal_Exception("Frame not found in cellmap");
  219. }
  220. $cols = $this->_frames[$key]["columns"];
  221. $w = 0;
  222. foreach ($cols as $i)
  223. $w += $this->_columns[$i]["used-width"];
  224. return $w;
  225. }
  226. function get_frame_height($frame) {
  227. $key = $frame->get_id();
  228. if ( !isset($this->_frames[$key]) )
  229. throw new DOMPDF_Internal_Exception("Frame not found in cellmap");
  230. $rows = $this->_frames[$key]["rows"];
  231. $h = 0;
  232. foreach ($rows as $i) {
  233. if ( !isset($this->_rows[$i]) ) {
  234. throw new Exception("foo");
  235. }
  236. $h += $this->_rows[$i]["height"];
  237. }
  238. return $h;
  239. }
  240. //........................................................................
  241. function set_column_width($j, $width) {
  242. $col =& $this->get_column($j);
  243. $col["used-width"] = $width;
  244. $next_col =& $this->get_column($j+1);
  245. $next_col["x"] = $next_col["x"] + $width;
  246. }
  247. function set_row_height($i, $height) {
  248. $row =& $this->get_row($i);
  249. if ( $height <= $row["height"] )
  250. return;
  251. $row["height"] = $height;
  252. $next_row =& $this->get_row($i+1);
  253. $next_row["y"] = $row["y"] + $height;
  254. }
  255. //........................................................................
  256. protected function _resolve_border($i, $j, $h_v, $border_spec) {
  257. $n_width = $border_spec["width"];
  258. $n_style = $border_spec["style"];
  259. $n_color = $border_spec["color"];
  260. if ( !isset($this->_borders[$i][$j][$h_v]) ) {
  261. $this->_borders[$i][$j][$h_v] = $border_spec;
  262. return $this->_borders[$i][$j][$h_v]["width"];
  263. }
  264. $o_width = $this->_borders[$i][$j][$h_v]["width"];
  265. $o_style = $this->_borders[$i][$j][$h_v]["style"];
  266. $o_color = $this->_borders[$i][$j][$h_v]["color"];
  267. if ( ($n_style === "hidden" ||
  268. $n_width > $o_width ||
  269. $o_style === "none")
  270. or
  271. ($o_width == $n_width &&
  272. in_array($n_style, self::$_BORDER_STYLE_SCORE) &&
  273. self::$_BORDER_STYLE_SCORE[ $n_style ] > self::$_BORDER_STYLE_SCORE[ $o_style ]) )
  274. $this->_borders[$i][$j][$h_v] = $border_spec;
  275. return $this->_borders[$i][$j][$h_v]["width"];
  276. }
  277. //........................................................................
  278. function add_frame(Frame $frame) {
  279. $style = $frame->get_style();
  280. $display = $style->display;
  281. $collapse = $this->_table->get_style()->border_collapse == "collapse";
  282. // Recursively add the frames within tables, table-row-groups and table-rows
  283. if ( $display == "table-row" ||
  284. $display == "table" ||
  285. $display == "inline-table" ||
  286. in_array($display, Table_Frame_Decorator::$ROW_GROUPS) ) {
  287. $start_row = $this->__row;
  288. foreach ( $frame->get_children() as $child )
  289. $this->add_frame( $child );
  290. if ( $display == "table-row" )
  291. $this->add_row();
  292. $num_rows = $this->__row - $start_row - 1;
  293. $key = $frame->get_id();
  294. // Row groups always span across the entire table
  295. $this->_frames[ $key ]["columns"] = range(0,max(0,$this->_num_cols-1));
  296. $this->_frames[ $key ]["rows"] = range($start_row, max(0, $this->__row - 1));
  297. $this->_frames[ $key ]["frame"] = $frame;
  298. if ( $display != "table-row" && $collapse ) {
  299. $bp = $style->get_border_properties();
  300. // Resolve the borders
  301. for ( $i = 0; $i < $num_rows+1; $i++) {
  302. $this->_resolve_border($start_row + $i, 0, "vertical", $bp["left"]);
  303. $this->_resolve_border($start_row + $i, $this->_num_cols, "vertical", $bp["right"]);
  304. }
  305. for ( $j = 0; $j < $this->_num_cols; $j++) {
  306. $this->_resolve_border($start_row, $j, "horizontal", $bp["top"]);
  307. $this->_resolve_border($this->__row, $j, "horizontal", $bp["bottom"]);
  308. }
  309. }
  310. return;
  311. }
  312. // Determine where this cell is going
  313. $colspan = $frame->get_node()->getAttribute("colspan");
  314. $rowspan = $frame->get_node()->getAttribute("rowspan");
  315. if ( !$colspan ) {
  316. $colspan = 1;
  317. $frame->get_node()->setAttribute("colspan",1);
  318. }
  319. if ( !$rowspan ) {
  320. $rowspan = 1;
  321. $frame->get_node()->setAttribute("rowspan",1);
  322. }
  323. $key = $frame->get_id();
  324. $bp = $style->get_border_properties();
  325. // Add the frame to the cellmap
  326. $max_left = $max_right = 0;
  327. // Find the next available column (fix by Ciro Mondueri)
  328. $ac = $this->__col;
  329. while ( isset($this->_cells[$this->__row][$ac]) )
  330. $ac++;
  331. $this->__col = $ac;
  332. // Rows:
  333. for ( $i = 0; $i < $rowspan; $i++ ) {
  334. $row = $this->__row + $i;
  335. $this->_frames[ $key ]["rows"][] = $row;
  336. for ( $j = 0; $j < $colspan; $j++)
  337. $this->_cells[$row][$this->__col + $j] = $frame;
  338. if ( $collapse ) {
  339. // Resolve vertical borders
  340. $max_left = max($max_left, $this->_resolve_border($row, $this->__col, "vertical", $bp["left"]));
  341. $max_right = max($max_right, $this->_resolve_border($row, $this->__col + $colspan, "vertical", $bp["right"]));
  342. }
  343. }
  344. $max_top = $max_bottom = 0;
  345. // Columns:
  346. for ( $j = 0; $j < $colspan; $j++ ) {
  347. $col = $this->__col + $j;
  348. $this->_frames[ $key ]["columns"][] = $col;
  349. if ( $collapse ) {
  350. // Resolve horizontal borders
  351. $max_top = max($max_top, $this->_resolve_border($this->__row, $col, "horizontal", $bp["top"]));
  352. $max_bottom = max($max_bottom, $this->_resolve_border($this->__row + $rowspan, $col, "horizontal", $bp["bottom"]));
  353. }
  354. }
  355. $this->_frames[ $key ]["frame"] = $frame;
  356. // Handle seperated border model
  357. if ( !$collapse ) {
  358. list($h, $v) = $this->_table->get_style()->border_spacing;
  359. // Border spacing is effectively a margin between cells
  360. $v = $style->length_in_pt($v) / 2;
  361. $h = $style->length_in_pt($h) / 2;
  362. $style->margin = "$v $h";
  363. // The additional 1/2 width gets added to the table proper
  364. } else {
  365. // Drop the frame's actual border
  366. $style->border_left_width = $max_left / 2;
  367. $style->border_right_width = $max_right / 2;
  368. $style->border_top_width = $max_top / 2;
  369. $style->border_bottom_width = $max_bottom / 2;
  370. $style->margin = "none";
  371. }
  372. // Resolve the frame's width
  373. list($frame_min, $frame_max) = $frame->get_min_max_width();
  374. $width = $style->width;
  375. if ( is_percent($width) ) {
  376. $var = "percent";
  377. $val = (float)rtrim($width, "% ") / $colspan;
  378. } else if ( $width !== "auto" ) {
  379. $var = "absolute";
  380. $val = $style->length_in_pt($frame_min) / $colspan;
  381. }
  382. $min = 0;
  383. $max = 0;
  384. for ( $cs = 0; $cs < $colspan; $cs++ ) {
  385. // Resolve the frame's width(s) with other cells
  386. $col =& $this->get_column( $this->__col + $cs );
  387. // Note: $var is either 'percent' or 'absolute'. We compare the
  388. // requested percentage or absolute values with the existing widths
  389. // and adjust accordingly.
  390. if ( isset($var) && $val > $col[$var] ) {
  391. $col[$var] = $val;
  392. $col["auto"] = false;
  393. }
  394. $min += $col["min-width"];
  395. $max += $col["max-width"];
  396. }
  397. if ( $frame_min > $min ) {
  398. // The frame needs more space. Expand each sub-column
  399. $inc = ($frame_min - $min) / $colspan;
  400. for ($c = 0; $c < $colspan; $c++) {
  401. $col =& $this->get_column($this->__col + $c);
  402. $col["min-width"] += $inc;
  403. }
  404. }
  405. if ( $frame_max > $max ) {
  406. $inc = ($frame_max - $max) / $colspan;
  407. for ($c = 0; $c < $colspan; $c++) {
  408. $col =& $this->get_column($this->__col + $c);
  409. $col["max-width"] += $inc;
  410. }
  411. }
  412. $this->__col += $colspan;
  413. if ( $this->__col > $this->_num_cols )
  414. $this->_num_cols = $this->__col;
  415. }
  416. //........................................................................
  417. function add_row() {
  418. $this->__row++;
  419. $this->_num_rows++;
  420. // Find the next available column
  421. $i = 0;
  422. while ( isset($this->_cells[$this->__row][$i]) )
  423. $i++;
  424. $this->__col = $i;
  425. }
  426. //........................................................................
  427. /**
  428. * Remove a row from the cellmap.
  429. *
  430. * @param Frame
  431. */
  432. function remove_row(Frame $row) {
  433. $key = $row->get_id();
  434. if ( !isset($this->_frames[$key]) )
  435. return; // Presumably this row has alredy been removed
  436. $this->_row = $this->_num_rows--;
  437. $rows = $this->_frames[$key]["rows"];
  438. $columns = $this->_frames[$key]["columns"];
  439. // Remove all frames from this row
  440. foreach ( $rows as $r ) {
  441. foreach ( $columns as $c ) {
  442. if ( isset($this->_cells[$r][$c]) ) {
  443. $frame = $this->_cells[$r][$c];
  444. unset($this->_frames[ $frame->get_id() ]);
  445. unset($this->_cells[$r][$c]);
  446. }
  447. }
  448. unset($this->_rows[$r]);
  449. }
  450. unset($this->_frames[$key]);
  451. }
  452. /**
  453. * Remove a row group from the cellmap.
  454. *
  455. * @param Frame $group The group to remove
  456. */
  457. function remove_row_group(Frame $group) {
  458. $key = $group->get_id();
  459. if ( !isset($this->_frames[$key]) )
  460. return; // Presumably this row has alredy been removed
  461. $iter = $group->get_first_child();
  462. while ($iter) {
  463. $this->remove_row($iter);
  464. $iter = $iter->get_next_sibling();
  465. }
  466. unset($this->_frames[$key]);
  467. }
  468. /**
  469. * Update a row group after rows have been removed
  470. *
  471. * @param Frame $group The group to update
  472. * @param Frame $last_row The last row in the row group
  473. */
  474. function update_row_group(Frame $group, Frame $last_row) {
  475. $g_key = $group->get_id();
  476. $r_key = $last_row->get_id();
  477. $r_rows = $this->_frames[$r_key]["rows"];
  478. $this->_frames[$g_key]["rows"] = range( $this->_frames[$g_key]["rows"][0], end($r_rows) );
  479. }
  480. //........................................................................
  481. function assign_x_positions() {
  482. // Pre-condition: widths must be resolved and assigned to columns and
  483. // column[0]["x"] must be set.
  484. $x = $this->_columns[0]["x"];
  485. foreach ( array_keys($this->_columns) as $j ) {
  486. $this->_columns[$j]["x"] = $x;
  487. $x += $this->_columns[$j]["used-width"];
  488. }
  489. }
  490. function assign_frame_heights() {
  491. // Pre-condition: widths and heights of each column & row must be
  492. // calcluated
  493. foreach ( $this->_frames as $arr ) {
  494. $frame = $arr["frame"];
  495. $h = 0;
  496. foreach( $arr["rows"] as $row ) {
  497. if ( !isset($this->_rows[$row]) )
  498. // The row has been removed because of a page split, so skip it.
  499. continue;
  500. $h += $this->_rows[$row]["height"];
  501. }
  502. if ( $frame instanceof Table_Cell_Frame_Decorator )
  503. $frame->set_cell_height($h);
  504. else
  505. $frame->get_style()->height = $h;
  506. }
  507. }
  508. //........................................................................
  509. /**
  510. * Re-adjust frame height if the table height is larger than its content
  511. */
  512. function set_frame_heights($table_height, $content_height) {
  513. // Distribute the increased height proportionally amongst each row
  514. foreach ( $this->_frames as $arr ) {
  515. $frame = $arr["frame"];
  516. $h = 0;
  517. foreach ($arr["rows"] as $row ) {
  518. if ( !isset($this->_rows[$row]) )
  519. continue;
  520. $h += $this->_rows[$row]["height"];
  521. }
  522. $new_height = ($h / $content_height) * $table_height;
  523. if ( $frame instanceof Table_Cell_Frame_Decorator )
  524. $frame->set_cell_height($new_height);
  525. else
  526. $frame->get_style()->height = $new_height;
  527. }
  528. }
  529. //........................................................................
  530. // Used for debugging:
  531. function __toString() {
  532. $str = "";
  533. $str .= "Columns:<br/>";
  534. $str .= pre_r($this->_columns, true);
  535. $str .= "Rows:<br/>";
  536. $str .= pre_r($this->_rows, true);
  537. $str .= "Frames:<br/>";
  538. $arr = array();
  539. foreach ( $this->_frames as $key => $val )
  540. $arr[$key] = array("columns" => $val["columns"], "rows" => $val["rows"]);
  541. $str .= pre_r($arr, true);
  542. if ( php_sapi_name() == "cli" )
  543. $str = strip_tags(str_replace(array("<br/>","<b>","</b>"),
  544. array("\n",chr(27)."[01;33m", chr(27)."[0m"),
  545. $str));
  546. return $str;
  547. }
  548. }