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,v 1.8 2006/07/07 21:31:02 benjcarson Exp $ */ |
---|
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 | function add_frame_to_line(Frame $frame) { |
---|
126 | |
---|
127 | // Handle inline frames (which are effectively wrappers) |
---|
128 | if ( $frame instanceof Inline_Frame_Decorator ) { |
---|
129 | |
---|
130 | // Handle line breaks |
---|
131 | if ( $frame->get_node()->nodeName == "br" ) { |
---|
132 | $this->maximize_line_height( $frame->get_style()->length_in_pt($frame->get_style()->line_height) ); |
---|
133 | $this->add_line(); |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | // Add each child of the inline frame to the line individually |
---|
138 | foreach ($frame->get_children() as $child) |
---|
139 | $this->add_frame_to_line( $child ); |
---|
140 | |
---|
141 | return; |
---|
142 | } |
---|
143 | |
---|
144 | $w = $frame->get_margin_width(); |
---|
145 | |
---|
146 | if ( $w == 0 ) |
---|
147 | return; |
---|
148 | |
---|
149 | // Debugging code: |
---|
150 | |
---|
151 | // pre_r("\nAdding frame to line:"); |
---|
152 | |
---|
153 | // pre_r("Me: " . $this->get_node()->nodeName . " (" . (string)$this->get_node() . ")"); |
---|
154 | // pre_r("Node: " . $frame->get_node()->nodeName . " (" . (string)$frame->get_node() . ")"); |
---|
155 | // if ( $frame->get_node()->nodeName == "#text" ) |
---|
156 | // pre_r($frame->get_node()->nodeValue); |
---|
157 | |
---|
158 | // pre_r("Line width: " . $this->_lines[$this->_cl]["w"]); |
---|
159 | // pre_r("Frame width: " . $w); |
---|
160 | // pre_r("Frame height: " . $frame->get_margin_height()); |
---|
161 | // pre_r("Containing block width: " . $this->get_containing_block("w")); |
---|
162 | |
---|
163 | // End debugging |
---|
164 | |
---|
165 | if ($this->_lines[$this->_cl]["w"] + $w > $this->get_containing_block("w")) |
---|
166 | $this->add_line(); |
---|
167 | |
---|
168 | $frame->position(); |
---|
169 | |
---|
170 | |
---|
171 | $this->_lines[$this->_cl]["frames"][] = $frame; |
---|
172 | |
---|
173 | if ( $frame->get_node()->nodeName == "#text") |
---|
174 | $this->_lines[$this->_cl]["wc"] += count(preg_split("/\s+/", $frame->get_text())); |
---|
175 | |
---|
176 | $this->_lines[$this->_cl]["w"] += $w; |
---|
177 | $this->_lines[$this->_cl]["h"] = max($this->_lines[$this->_cl]["h"], $frame->get_margin_height()); |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | function remove_frames_from_line(Frame $frame) { |
---|
182 | // Search backwards through the lines for $frame |
---|
183 | $i = $this->_cl; |
---|
184 | |
---|
185 | while ($i >= 0) { |
---|
186 | if ( ($j = in_array($frame, $this->_lines[$i]["frames"], true)) !== false ) |
---|
187 | break; |
---|
188 | $i--; |
---|
189 | } |
---|
190 | |
---|
191 | if ( $j === false ) |
---|
192 | return; |
---|
193 | |
---|
194 | // Remove $frame and all frames that follow |
---|
195 | while ($j < count($this->_lines[$i]["frames"])) { |
---|
196 | $f = $this->_lines[$i]["frames"][$j]; |
---|
197 | unset($this->_lines[$i]["frames"][$j++]); |
---|
198 | $this->_lines[$i]["w"] -= $f->get_margin_width(); |
---|
199 | } |
---|
200 | |
---|
201 | // Recalculate the height of the line |
---|
202 | $h = 0; |
---|
203 | foreach ($this->_lines[$i]["frames"] as $f) |
---|
204 | $h = max( $h, $f->get_margin_height() ); |
---|
205 | |
---|
206 | $this->_lines[$i]["h"] = $h; |
---|
207 | |
---|
208 | // Remove all lines that follow |
---|
209 | while ($this->_cl > $i) |
---|
210 | unset($this->_lines[ $this->_cl-- ]); |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | function increase_line_width($w) { |
---|
215 | $this->_lines[ $this->_cl ]["w"] += $w; |
---|
216 | } |
---|
217 | |
---|
218 | function maximize_line_height($val) { |
---|
219 | $this->_lines[ $this->_cl ]["h"] = max($this->_lines[ $this->_cl ]["h"], $val); |
---|
220 | } |
---|
221 | |
---|
222 | function add_line() { |
---|
223 | |
---|
224 | // if ( $this->_lines[$this->_cl]["h"] == 0 ) //count($this->_lines[$i]["frames"]) == 0 || |
---|
225 | // return; |
---|
226 | |
---|
227 | $y = $this->_lines[$this->_cl]["y"] + $this->_lines[$this->_cl]["h"]; |
---|
228 | |
---|
229 | $this->_lines[ ++$this->_cl ] = array("frames" => array(), |
---|
230 | "wc" => 0, |
---|
231 | "y" => $y, "w" => 0, "h" => 0); |
---|
232 | } |
---|
233 | |
---|
234 | //........................................................................ |
---|
235 | |
---|
236 | function reset_counter($id = self::DEFAULT_COUNTER, $value = 0) { |
---|
237 | $this->_counters[$id] = $value; |
---|
238 | } |
---|
239 | |
---|
240 | function increment_counter($id = self::DEFAULT_COUNTER, $increment = 1) { |
---|
241 | if ( !isset($this->_counters[$id]) ) |
---|
242 | $this->_counters[$id] = $increment; |
---|
243 | else |
---|
244 | $this->_counters[$id] += $increment; |
---|
245 | |
---|
246 | } |
---|
247 | |
---|
248 | function counter_value($id = self::DEFAULT_COUNTER, $type = "decimal") { |
---|
249 | $type = mb_strtolower($type); |
---|
250 | if ( !isset($this->_counters[$id]) ) |
---|
251 | $this->_counters[$id] = 0; |
---|
252 | |
---|
253 | switch ($type) { |
---|
254 | |
---|
255 | default: |
---|
256 | case "decimal": |
---|
257 | return $this->_counters[$id]; |
---|
258 | |
---|
259 | case "decimal-leading-zero": |
---|
260 | return str_pad($this->_counters[$id], 2, "0"); |
---|
261 | |
---|
262 | case "lower-roman": |
---|
263 | return dec2roman($this->_counters[$id]); |
---|
264 | |
---|
265 | case "upper-roman": |
---|
266 | return mb_strtoupper(dec2roman($this->_counters[$id])); |
---|
267 | |
---|
268 | case "lower-latin": |
---|
269 | case "lower-alpha": |
---|
270 | return chr( ($this->_counters[$id] % 26) + ord('a') - 1); |
---|
271 | |
---|
272 | case "upper-latin": |
---|
273 | case "upper-alpha": |
---|
274 | return chr( ($this->_counters[$id] % 26) + ord('A') - 1); |
---|
275 | |
---|
276 | case "lower-greek": |
---|
277 | return chr($this->_counters[$id] + 944); |
---|
278 | |
---|
279 | case "upper-greek": |
---|
280 | return chr($this->_counters[$id] + 912); |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | ?> |
---|