frame_reflower.cls.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: frame_reflower.cls.php,v $
  6. * Created on: 2004-06-17
  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: frame_reflower.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Base reflower class
  41. *
  42. * Reflower objects are responsible for determining the width and height of
  43. * individual frames. They also create line and page breaks as necessary.
  44. *
  45. * @access private
  46. * @package dompdf
  47. */
  48. abstract class Frame_Reflower {
  49. /**
  50. * Frame for this reflower
  51. *
  52. * @var Frame
  53. */
  54. protected $_frame;
  55. /**
  56. * Cached min/max size
  57. *
  58. * @var array
  59. */
  60. protected $_min_max_cache;
  61. function __construct(Frame $frame) {
  62. $this->_frame = $frame;
  63. $this->_min_max_cache = null;
  64. }
  65. function dispose() {
  66. unset($this->_frame);
  67. }
  68. protected function _collapse_margins() {
  69. $cb = $this->_frame->get_containing_block();
  70. $style = $this->_frame->get_style();
  71. $t = $style->length_in_pt($style->margin_top, $cb["h"]);
  72. $b = $style->length_in_pt($style->margin_bottom, $cb["w"]);
  73. // Handle 'auto' values
  74. if ( $t === "auto" ) {
  75. $style->margin_top = "0pt";
  76. $t = 0;
  77. }
  78. if ( $b === "auto" ) {
  79. $style->margin_bottom = "0pt";
  80. $b = 0;
  81. }
  82. // Collapse vertical margins:
  83. $n = $this->_frame->get_next_sibling();
  84. while ( $n && !in_array($n->get_style()->display, Style::$BLOCK_TYPES) )
  85. $n = $n->get_next_sibling();
  86. if ( $n ) { // && !$n instanceof Page_Frame_Decorator ) {
  87. $b = max($b, $style->length_in_pt($n->get_style()->margin_top, $cb["w"]));
  88. $n->get_style()->margin_top = "$b pt";
  89. $style->margin_bottom = "0 pt";
  90. }
  91. // Collapse our first child's margin
  92. $f = $this->_frame->get_first_child();
  93. while ( $f && !in_array($f->get_style()->display, Style::$BLOCK_TYPES) )
  94. $f = $f->get_next_sibling();
  95. if ( $f ) {
  96. $t = max( $t, $style->length_in_pt($f->get_style()->margin_top, $cb["w"]));
  97. $style->margin_top = "$t pt";
  98. $f->get_style()->margin_top = "0 pt";
  99. }
  100. }
  101. // Returns true if a new page is required
  102. protected function _check_new_page() {
  103. $y = $this->_frame->get_position("y");
  104. $h = $style->length_in_pt($style->height);
  105. // Check if we need to move to a new page
  106. if ( $y + $h >= $this->_frame->get_root()->get_page_height() )
  107. return true;
  108. }
  109. //........................................................................
  110. abstract function reflow();
  111. //........................................................................
  112. // Required for table layout: Returns an array(0 => min, 1 => max, "min"
  113. // => min, "max" => max) of the minimum and maximum widths of this frame.
  114. // This provides a basic implementation. Child classes should override
  115. // this if necessary.
  116. function get_min_max_width() {
  117. if ( !is_null($this->_min_max_cache) ) {
  118. return $this->_min_max_cache;
  119. }
  120. $style = $this->_frame->get_style();
  121. // Account for margins & padding
  122. $dims = array($style->padding_left,
  123. $style->padding_right,
  124. $style->border_left_width,
  125. $style->border_right_width,
  126. $style->margin_left,
  127. $style->margin_right);
  128. $cb_w = $this->_frame->get_containing_block("w");
  129. $delta = $style->length_in_pt($dims, $cb_w);
  130. // Handle degenerate case
  131. if ( !$this->_frame->get_first_child() )
  132. return $this->_min_max_cache = array($delta, $delta,"min" => $delta, "max" => $delta);
  133. $low = array();
  134. $high = array();
  135. for ( $iter = $this->_frame->get_children()->getIterator();
  136. $iter->valid();
  137. $iter->next() ) {
  138. $inline_min = 0;
  139. $inline_max = 0;
  140. // Add all adjacent inline widths together to calculate max width
  141. while ( $iter->valid() && in_array( $iter->current()->get_style()->display, Style::$INLINE_TYPES ) ) {
  142. $child = $iter->current();
  143. $minmax = $child->get_min_max_width();
  144. if ( in_array( $iter->current()->get_style()->white_space, array("pre", "nowrap") ) )
  145. $inline_min += $minmax["min"];
  146. else
  147. $low[] = $minmax["min"];
  148. $inline_max += $minmax["max"];
  149. $iter->next();
  150. }
  151. if ( $inline_max > 0 )
  152. $high[] = $inline_max;
  153. if ( $inline_min > 0 )
  154. $low[] = $inline_min;
  155. if ( $iter->valid() ) {
  156. list($low[], $high[]) = $iter->current()->get_min_max_width();
  157. continue;
  158. }
  159. }
  160. $min = count($low) ? max($low) : 0;
  161. $max = count($high) ? max($high) : 0;
  162. // Use specified width if it is greater than the minimum defined by the
  163. // content. If the width is a percentage ignore it for now.
  164. $width = $style->width;
  165. if ( $width !== "auto" && !is_percent($width) ) {
  166. $width = $style->length_in_pt($width, $cb_w);
  167. if ( $min < $width )
  168. $min = $width;
  169. if ( $max < $width )
  170. $max = $width;
  171. }
  172. $min += $delta;
  173. $max += $delta;
  174. return $this->_min_max_cache = array($min, $max, "min"=>$min, "max"=>$max);
  175. }
  176. }