[3019] | 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.digitaljunkies.ca/dompdf |
---|
| 32 | * |
---|
| 33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
| 34 | * @copyright 2004 Benj Carson |
---|
| 35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 36 | * @package dompdf |
---|
| 37 | * @version 0.5.1 |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* $Id: frame_reflower.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Base reflower class |
---|
| 44 | * |
---|
| 45 | * Reflower objects are responsible for determining the width and height of |
---|
| 46 | * individual frames. They also create line and page breaks as necessary. |
---|
| 47 | * |
---|
| 48 | * @access private |
---|
| 49 | * @package dompdf |
---|
| 50 | */ |
---|
| 51 | abstract class Frame_Reflower { |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Frame for this reflower |
---|
| 55 | * |
---|
| 56 | * @var Frame |
---|
| 57 | */ |
---|
| 58 | protected $_frame; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Cached min/max size |
---|
| 62 | * |
---|
| 63 | * @var array |
---|
| 64 | */ |
---|
| 65 | protected $_min_max_cache; |
---|
| 66 | |
---|
| 67 | function __construct(Frame $frame) { |
---|
| 68 | $this->_frame = $frame; |
---|
| 69 | $this->_min_max_cache = null; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | function dispose() { |
---|
| 73 | unset($this->_frame); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | protected function _collapse_margins() { |
---|
| 77 | $cb = $this->_frame->get_containing_block(); |
---|
| 78 | $style = $this->_frame->get_style(); |
---|
| 79 | |
---|
| 80 | $t = $style->length_in_pt($style->margin_top, $cb["h"]); |
---|
| 81 | $b = $style->length_in_pt($style->margin_bottom, $cb["w"]); |
---|
| 82 | |
---|
| 83 | // Handle 'auto' values |
---|
| 84 | if ( $t === "auto" ) { |
---|
| 85 | $style->margin_top = "0pt"; |
---|
| 86 | $t = 0; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if ( $b === "auto" ) { |
---|
| 90 | $style->margin_bottom = "0pt"; |
---|
| 91 | $b = 0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // Collapse vertical margins: |
---|
| 95 | $n = $this->_frame->get_next_sibling(); |
---|
| 96 | while ( $n && !in_array($n->get_style()->display, Style::$BLOCK_TYPES) ) |
---|
| 97 | $n = $n->get_next_sibling(); |
---|
| 98 | |
---|
| 99 | if ( $n ) { // && !$n instanceof Page_Frame_Decorator ) { |
---|
| 100 | |
---|
| 101 | $b = max($b, $style->length_in_pt($n->get_style()->margin_top, $cb["w"])); |
---|
| 102 | |
---|
| 103 | $n->get_style()->margin_top = "$b pt"; |
---|
| 104 | $style->margin_bottom = "0 pt"; |
---|
| 105 | |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // Collapse our first child's margin |
---|
| 109 | $f = $this->_frame->get_first_child(); |
---|
| 110 | while ( $f && !in_array($f->get_style()->display, Style::$BLOCK_TYPES) ) |
---|
| 111 | $f = $f->get_next_sibling(); |
---|
| 112 | |
---|
| 113 | if ( $f ) { |
---|
| 114 | $t = max( $t, $style->length_in_pt($f->get_style()->margin_top, $cb["w"])); |
---|
| 115 | $style->margin_top = "$t pt"; |
---|
| 116 | $f->get_style()->margin_top = "0 pt"; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | // Returns true if a new page is required |
---|
| 122 | protected function _check_new_page() { |
---|
| 123 | $y = $this->_frame->get_position("y"); |
---|
| 124 | $h = $style->length_in_pt($style->height); |
---|
| 125 | // Check if we need to move to a new page |
---|
| 126 | if ( $y + $h >= $this->_frame->get_root()->get_page_height() ) |
---|
| 127 | return true; |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | //........................................................................ |
---|
| 132 | |
---|
| 133 | abstract function reflow(); |
---|
| 134 | |
---|
| 135 | //........................................................................ |
---|
| 136 | |
---|
| 137 | // Required for table layout: Returns an array(0 => min, 1 => max, "min" |
---|
| 138 | // => min, "max" => max) of the minimum and maximum widths of this frame. |
---|
| 139 | // This provides a basic implementation. Child classes should override |
---|
| 140 | // this if necessary. |
---|
| 141 | function get_min_max_width() { |
---|
| 142 | if ( !is_null($this->_min_max_cache) ) { |
---|
| 143 | return $this->_min_max_cache; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | $style = $this->_frame->get_style(); |
---|
| 147 | |
---|
| 148 | // Account for margins & padding |
---|
| 149 | $dims = array($style->padding_left, |
---|
| 150 | $style->padding_right, |
---|
| 151 | $style->border_left_width, |
---|
| 152 | $style->border_right_width, |
---|
| 153 | $style->margin_left, |
---|
| 154 | $style->margin_right); |
---|
| 155 | |
---|
| 156 | $cb_w = $this->_frame->get_containing_block("w"); |
---|
| 157 | $delta = $style->length_in_pt($dims, $cb_w); |
---|
| 158 | |
---|
| 159 | // Handle degenerate case |
---|
| 160 | if ( !$this->_frame->get_first_child() ) |
---|
| 161 | return $this->_min_max_cache = array($delta, $delta,"min" => $delta, "max" => $delta); |
---|
| 162 | |
---|
| 163 | $low = array(); |
---|
| 164 | $high = array(); |
---|
| 165 | |
---|
| 166 | for ( $iter = $this->_frame->get_children()->getIterator(); |
---|
| 167 | $iter->valid(); |
---|
| 168 | $iter->next() ) { |
---|
| 169 | |
---|
| 170 | $inline_min = 0; |
---|
| 171 | $inline_max = 0; |
---|
| 172 | |
---|
| 173 | // Add all adjacent inline widths together to calculate max width |
---|
| 174 | while ( $iter->valid() && in_array( $iter->current()->get_style()->display, Style::$INLINE_TYPES ) ) { |
---|
| 175 | |
---|
| 176 | $child = $iter->current(); |
---|
| 177 | |
---|
| 178 | $minmax = $child->get_min_max_width(); |
---|
| 179 | |
---|
| 180 | if ( in_array( $iter->current()->get_style()->white_space, array("pre", "nowrap") ) ) |
---|
| 181 | $inline_min += $minmax["min"]; |
---|
| 182 | else |
---|
| 183 | $low[] = $minmax["min"]; |
---|
| 184 | |
---|
| 185 | $inline_max += $minmax["max"]; |
---|
| 186 | $iter->next(); |
---|
| 187 | |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if ( $inline_max > 0 ) |
---|
| 191 | $high[] = $inline_max; |
---|
| 192 | |
---|
| 193 | if ( $inline_min > 0 ) |
---|
| 194 | $low[] = $inline_min; |
---|
| 195 | |
---|
| 196 | if ( $iter->valid() ) { |
---|
| 197 | list($low[], $high[]) = $iter->current()->get_min_max_width(); |
---|
| 198 | continue; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | } |
---|
| 202 | $min = count($low) ? max($low) : 0; |
---|
| 203 | $max = count($high) ? max($high) : 0; |
---|
| 204 | |
---|
| 205 | // Use specified width if it is greater than the minimum defined by the |
---|
| 206 | // content. If the width is a percentage ignore it for now. |
---|
| 207 | $width = $style->width; |
---|
| 208 | if ( $width !== "auto" && !is_percent($width) ) { |
---|
| 209 | $width = $style->length_in_pt($width, $cb_w); |
---|
| 210 | if ( $min < $width ) |
---|
| 211 | $min = $width; |
---|
| 212 | if ( $max < $width ) |
---|
| 213 | $max = $width; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | $min += $delta; |
---|
| 217 | $max += $delta; |
---|
| 218 | return $this->_min_max_cache = array($min, $max, "min"=>$min, "max"=>$max); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | ?> |
---|