[2000] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer
|
---|
| 4 | *
|
---|
| 5 | * File: $RCSfile: block_frame_decorator.cls.php,v $
|
---|
| 6 | * Created on: 2004-06-02
|
---|
| 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: block_frame_decorator.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Decorates frames for block layout
|
---|
| 44 | *
|
---|
| 45 | * @access private
|
---|
| 46 | * @package dompdf
|
---|
| 47 | */
|
---|
| 48 | class Block_Frame_Decorator extends Frame_Decorator {
|
---|
| 49 |
|
---|
| 50 | const DEFAULT_COUNTER = "-dompdf-default-counter";
|
---|
| 51 |
|
---|
| 52 | protected $_lines; // array( [num] => array([frames] => array(<frame list>),
|
---|
| 53 | // y, w, h) )
|
---|
| 54 | protected $_counters; // array([id] => counter_value) (for generated content)
|
---|
| 55 | protected $_cl; // current line index
|
---|
| 56 |
|
---|
| 57 | //........................................................................
|
---|
| 58 |
|
---|
| 59 | function __construct(Frame $frame, DOMPDF $dompdf) {
|
---|
| 60 | parent::__construct($frame, $dompdf);
|
---|
| 61 | $this->_lines = array(array("frames" => array(),
|
---|
| 62 | "wc" => 0,
|
---|
| 63 | "y" => null,
|
---|
| 64 | "w" => 0,
|
---|
| 65 | "h" => 0));
|
---|
| 66 | $this->_counters = array(self::DEFAULT_COUNTER => 0);
|
---|
| 67 | $this->_cl = 0;
|
---|
| 68 |
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | //........................................................................
|
---|
| 72 |
|
---|
| 73 | function reset() {
|
---|
| 74 | parent::reset();
|
---|
| 75 | $this->_lines = array(array("frames" => array(),
|
---|
| 76 | "wc" => 0,
|
---|
| 77 | "y" => null,
|
---|
| 78 | "w" => 0,
|
---|
| 79 | "h" => 0));
|
---|
| 80 | $this->_counters = array(self::DEFAULT_COUNTER => 0);
|
---|
| 81 | $this->_cl = 0;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | //........................................................................
|
---|
| 85 |
|
---|
| 86 | // Accessor methods
|
---|
| 87 |
|
---|
| 88 | function get_current_line($i = null) {
|
---|
| 89 | $cl = $this->_lines[$this->_cl];
|
---|
| 90 | if ( isset($i) )
|
---|
| 91 | return $cl[$i];
|
---|
| 92 | return $cl;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | function get_lines() { return $this->_lines; }
|
---|
| 96 |
|
---|
| 97 | //........................................................................
|
---|
| 98 |
|
---|
| 99 | // Set methods
|
---|
| 100 | function set_current_line($y = null, $w = null, $h = null) {
|
---|
| 101 | $this->set_line($this->_cl, $y, $w, $h);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | function clear_line($i) {
|
---|
| 105 | if ( isset($this->_lines[$i]) )
|
---|
| 106 | unset($this->_lines[$i]);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | function set_line($lineno, $y = null, $w = null, $h = null) {
|
---|
| 110 |
|
---|
| 111 | if ( is_array($y) )
|
---|
| 112 | extract($y);
|
---|
| 113 |
|
---|
| 114 | if (is_numeric($y))
|
---|
| 115 | $this->_lines[$lineno]["y"] = $y;
|
---|
| 116 |
|
---|
| 117 | if (is_numeric($w))
|
---|
| 118 | $this->_lines[$lineno]["w"] = $w;
|
---|
| 119 |
|
---|
| 120 | if (is_numeric($h))
|
---|
| 121 | $this->_lines[$lineno]["h"] = $h;
|
---|
| 122 |
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | function add_frame_to_line(Frame $frame) {
|
---|
| 127 |
|
---|
| 128 | // Handle inline frames (which are effectively wrappers)
|
---|
| 129 | if ( $frame instanceof Inline_Frame_Decorator ) {
|
---|
| 130 |
|
---|
| 131 | // Handle line breaks
|
---|
| 132 | if ( $frame->get_node()->nodeName == "br" ) {
|
---|
| 133 | $this->maximize_line_height( $frame->get_style()->length_in_pt($frame->get_style()->line_height) );
|
---|
| 134 | $this->add_line();
|
---|
| 135 | return;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | // Add each child of the inline frame to the line individually
|
---|
| 139 | foreach ($frame->get_children() as $child)
|
---|
| 140 | $this->add_frame_to_line( $child );
|
---|
| 141 |
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | // Trim leading text if this is an empty line. Kinda a hack to put it here,
|
---|
| 146 | // but what can you do...
|
---|
| 147 | if ( $this->_lines[$this->_cl]["w"] == 0 &&
|
---|
| 148 | $frame->get_node()->nodeName == "#text" &&
|
---|
| 149 | ($frame->get_style()->white_space != "pre" ||
|
---|
| 150 | $frame->get_style()->white_space != "pre-wrap") ) {
|
---|
| 151 |
|
---|
| 152 | $frame->set_text( ltrim($frame->get_text()) );
|
---|
| 153 | $frame->recalculate_width();
|
---|
| 154 |
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | $w = $frame->get_margin_width();
|
---|
| 158 |
|
---|
| 159 | if ( $w == 0 )
|
---|
| 160 | return;
|
---|
| 161 |
|
---|
| 162 | // Debugging code:
|
---|
| 163 | /*
|
---|
| 164 | pre_r("\nAdding frame to line:");
|
---|
| 165 |
|
---|
| 166 | // pre_r("Me: " . $this->get_node()->nodeName . " (" . spl_object_hash($this->get_node()) . ")");
|
---|
| 167 | // pre_r("Node: " . $frame->get_node()->nodeName . " (" . spl_object_hash($frame->get_node()) . ")");
|
---|
| 168 | if ( $frame->get_node()->nodeName == "#text" )
|
---|
| 169 | pre_r($frame->get_node()->nodeValue);
|
---|
| 170 |
|
---|
| 171 | pre_r("Line width: " . $this->_lines[$this->_cl]["w"]);
|
---|
| 172 | pre_r("Frame: " . get_class($frame));
|
---|
| 173 | pre_r("Frame width: " . $w);
|
---|
| 174 | pre_r("Frame height: " . $frame->get_margin_height());
|
---|
| 175 | pre_r("Containing block width: " . $this->get_containing_block("w"));
|
---|
| 176 | */
|
---|
| 177 | // End debugging
|
---|
| 178 |
|
---|
| 179 | if ($this->_lines[$this->_cl]["w"] + $w > $this->get_containing_block("w"))
|
---|
| 180 | $this->add_line();
|
---|
| 181 |
|
---|
| 182 | $frame->position();
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | $this->_lines[$this->_cl]["frames"][] = $frame;
|
---|
| 186 |
|
---|
| 187 | if ( $frame->get_node()->nodeName == "#text")
|
---|
| 188 | $this->_lines[$this->_cl]["wc"] += count(preg_split("/\s+/", $frame->get_text()));
|
---|
| 189 |
|
---|
| 190 | $this->_lines[$this->_cl]["w"] += $w;
|
---|
| 191 | $this->_lines[$this->_cl]["h"] = max($this->_lines[$this->_cl]["h"], $frame->get_margin_height());
|
---|
| 192 |
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | function remove_frames_from_line(Frame $frame) {
|
---|
| 196 | // Search backwards through the lines for $frame
|
---|
| 197 | $i = $this->_cl;
|
---|
| 198 |
|
---|
| 199 | while ($i >= 0) {
|
---|
| 200 | if ( ($j = in_array($frame, $this->_lines[$i]["frames"], true)) !== false )
|
---|
| 201 | break;
|
---|
| 202 | $i--;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | if ( $j === false )
|
---|
| 206 | return;
|
---|
| 207 |
|
---|
| 208 | // Remove $frame and all frames that follow
|
---|
| 209 | while ($j < count($this->_lines[$i]["frames"])) {
|
---|
| 210 | $f = $this->_lines[$i]["frames"][$j];
|
---|
| 211 | unset($this->_lines[$i]["frames"][$j++]);
|
---|
| 212 | $this->_lines[$i]["w"] -= $f->get_margin_width();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | // Recalculate the height of the line
|
---|
| 216 | $h = 0;
|
---|
| 217 | foreach ($this->_lines[$i]["frames"] as $f)
|
---|
| 218 | $h = max( $h, $f->get_margin_height() );
|
---|
| 219 |
|
---|
| 220 | $this->_lines[$i]["h"] = $h;
|
---|
| 221 |
|
---|
| 222 | // Remove all lines that follow
|
---|
| 223 | while ($this->_cl > $i)
|
---|
| 224 | unset($this->_lines[ $this->_cl-- ]);
|
---|
| 225 |
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | function increase_line_width($w) {
|
---|
| 229 | $this->_lines[ $this->_cl ]["w"] += $w;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | function maximize_line_height($val) {
|
---|
| 233 | $this->_lines[ $this->_cl ]["h"] = max($this->_lines[ $this->_cl ]["h"], $val);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | function add_line() {
|
---|
| 237 |
|
---|
| 238 | // if ( $this->_lines[$this->_cl]["h"] == 0 ) //count($this->_lines[$i]["frames"]) == 0 ||
|
---|
| 239 | // return;
|
---|
| 240 |
|
---|
| 241 | $y = $this->_lines[$this->_cl]["y"] + $this->_lines[$this->_cl]["h"];
|
---|
| 242 |
|
---|
| 243 | $this->_lines[ ++$this->_cl ] = array("frames" => array(),
|
---|
| 244 | "wc" => 0,
|
---|
| 245 | "y" => $y, "w" => 0, "h" => 0);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | //........................................................................
|
---|
| 249 |
|
---|
| 250 | function reset_counter($id = self::DEFAULT_COUNTER, $value = 0) {
|
---|
| 251 | $this->_counters[$id] = $value;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | function increment_counter($id = self::DEFAULT_COUNTER, $increment = 1) {
|
---|
| 255 | if ( !isset($this->_counters[$id]) )
|
---|
| 256 | $this->_counters[$id] = $increment;
|
---|
| 257 | else
|
---|
| 258 | $this->_counters[$id] += $increment;
|
---|
| 259 |
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | function counter_value($id = self::DEFAULT_COUNTER, $type = "decimal") {
|
---|
| 263 | $type = mb_strtolower($type);
|
---|
| 264 | if ( !isset($this->_counters[$id]) )
|
---|
| 265 | $this->_counters[$id] = 0;
|
---|
| 266 |
|
---|
| 267 | switch ($type) {
|
---|
| 268 |
|
---|
| 269 | default:
|
---|
| 270 | case "decimal":
|
---|
| 271 | return $this->_counters[$id];
|
---|
| 272 |
|
---|
| 273 | case "decimal-leading-zero":
|
---|
| 274 | return str_pad($this->_counters[$id], 2, "0");
|
---|
| 275 |
|
---|
| 276 | case "lower-roman":
|
---|
| 277 | return dec2roman($this->_counters[$id]);
|
---|
| 278 |
|
---|
| 279 | case "upper-roman":
|
---|
| 280 | return mb_strtoupper(dec2roman($this->_counters[$id]));
|
---|
| 281 |
|
---|
| 282 | case "lower-latin":
|
---|
| 283 | case "lower-alpha":
|
---|
| 284 | return chr( ($this->_counters[$id] % 26) + ord('a') - 1);
|
---|
| 285 |
|
---|
| 286 | case "upper-latin":
|
---|
| 287 | case "upper-alpha":
|
---|
| 288 | return chr( ($this->_counters[$id] % 26) + ord('A') - 1);
|
---|
| 289 |
|
---|
| 290 | case "lower-greek":
|
---|
| 291 | return chr($this->_counters[$id] + 944);
|
---|
| 292 |
|
---|
| 293 | case "upper-greek":
|
---|
| 294 | return chr($this->_counters[$id] + 912);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | ?> |
---|