cellmap.cls.php 19 KB

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