1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: 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: frame_decorator.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
41 | |
---|
42 | /** |
---|
43 | * Base Frame_Decorator class |
---|
44 | * |
---|
45 | * @access private |
---|
46 | * @package dompdf |
---|
47 | */ |
---|
48 | abstract class Frame_Decorator extends Frame { |
---|
49 | |
---|
50 | /** |
---|
51 | * The root node of the DOM tree |
---|
52 | * |
---|
53 | * @var Frame |
---|
54 | */ |
---|
55 | protected $_root; |
---|
56 | |
---|
57 | /** |
---|
58 | * The decorated frame |
---|
59 | * |
---|
60 | * @var Frame |
---|
61 | */ |
---|
62 | protected $_frame; |
---|
63 | |
---|
64 | /** |
---|
65 | * Positioner object used to position this frame (Strategy pattern) |
---|
66 | * |
---|
67 | * @var Positioner |
---|
68 | */ |
---|
69 | protected $_positioner; |
---|
70 | |
---|
71 | /** |
---|
72 | * Reflower object used to calculate frame dimensions (Strategy pattern) |
---|
73 | * |
---|
74 | * @var Frame_Reflower |
---|
75 | */ |
---|
76 | protected $_reflower; |
---|
77 | |
---|
78 | /** |
---|
79 | * Reference to the current dompdf instance |
---|
80 | * |
---|
81 | * @var DOMPDF |
---|
82 | */ |
---|
83 | protected $_dompdf; |
---|
84 | |
---|
85 | /** |
---|
86 | * Class constructor |
---|
87 | * |
---|
88 | * @param Frame $frame the decoration target |
---|
89 | */ |
---|
90 | function __construct(Frame $frame, DOMPDF $dompdf) { |
---|
91 | $this->_frame = $frame; |
---|
92 | $this->_root = null; |
---|
93 | $this->_dompdf = $dompdf; |
---|
94 | $frame->set_decorator($this); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * "Destructor": foribly free all references held by this object |
---|
99 | * |
---|
100 | * @param bool $recursive if true, call dispose on all children |
---|
101 | */ |
---|
102 | function dispose($recursive = false) { |
---|
103 | |
---|
104 | if ( $recursive ) { |
---|
105 | while ( $child = $this->get_first_child() ) |
---|
106 | $child->dispose(true); |
---|
107 | } |
---|
108 | |
---|
109 | unset($this->_root); |
---|
110 | $this->_frame->dispose(false); |
---|
111 | unset($this->_frame); |
---|
112 | unset($this->_positioner); |
---|
113 | unset($this->_reflower); |
---|
114 | |
---|
115 | } |
---|
116 | |
---|
117 | // Return a copy of this frame with $node as its node |
---|
118 | function copy(DomNode $node) { |
---|
119 | $frame = new Frame($node); |
---|
120 | $frame->set_style(clone $this->_frame->get_original_style()); |
---|
121 | $deco = Frame_Factory::decorate_frame($frame, $this->_dompdf); |
---|
122 | $deco->set_root($this->_root); |
---|
123 | return $deco; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Create a deep copy: copy this node and all children |
---|
128 | * |
---|
129 | * @return Frame |
---|
130 | */ |
---|
131 | function deep_copy() { |
---|
132 | $frame = new Frame($this->get_node()->cloneNode()); |
---|
133 | $frame->set_style(clone $this->_frame->get_original_style()); |
---|
134 | $deco = Frame_Factory::decorate_frame($frame, $this->_dompdf); |
---|
135 | $deco->set_root($this->_root); |
---|
136 | |
---|
137 | foreach ($this->get_children() as $child) |
---|
138 | $deco->append_child($child->deep_copy()); |
---|
139 | |
---|
140 | return $deco; |
---|
141 | } |
---|
142 | //........................................................................ |
---|
143 | |
---|
144 | // Delegate calls to decorated frame object |
---|
145 | function reset() { |
---|
146 | $this->_frame->reset(); |
---|
147 | |
---|
148 | // Reset all children |
---|
149 | foreach ($this->get_children() as $child) |
---|
150 | $child->reset(); |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | function get_node() { return $this->_frame->get_node(); } |
---|
155 | function get_id() { return $this->_frame->get_id(); } |
---|
156 | function get_style() { return $this->_frame->get_style(); } |
---|
157 | function get_original_style() { return $this->_frame->get_original_style(); } |
---|
158 | function get_containing_block($i = null) { return $this->_frame->get_containing_block($i); } |
---|
159 | function get_position($i = null) { return $this->_frame->get_position($i); } |
---|
160 | function get_dompdf() { return $this->_dompdf; } |
---|
161 | // function get_decorator() { |
---|
162 | // if ( isset($this->_decorator) ) |
---|
163 | // return $this->_decorator; |
---|
164 | // else |
---|
165 | // return $this; |
---|
166 | // } |
---|
167 | |
---|
168 | function get_margin_height() { return $this->_frame->get_margin_height(); } |
---|
169 | function get_margin_width() { return $this->_frame->get_margin_width(); } |
---|
170 | function get_padding_box() { return $this->_frame->get_padding_box(); } |
---|
171 | function get_border_box() { return $this->_frame->get_border_box(); } |
---|
172 | |
---|
173 | function set_id($id) { $this->_frame->set_id($id); } |
---|
174 | function set_style(Style $style) { $this->_frame->set_style($style); } |
---|
175 | |
---|
176 | function set_containing_block($x = null, $y = null, $w = null, $h = null) { |
---|
177 | $this->_frame->set_containing_block($x, $y, $w, $h); |
---|
178 | } |
---|
179 | |
---|
180 | function set_position($x = null, $y = null) { |
---|
181 | $this->_frame->set_position($x, $y); |
---|
182 | } |
---|
183 | function __toString() { return $this->_frame->__toString(); } |
---|
184 | |
---|
185 | function prepend_child(Frame $child, $update_node = true) { |
---|
186 | while ( $child instanceof Frame_Decorator ) |
---|
187 | $child = $child->_frame; |
---|
188 | |
---|
189 | $this->_frame->prepend_child($child, $update_node); |
---|
190 | } |
---|
191 | |
---|
192 | function append_child(Frame $child, $update_node = true) { |
---|
193 | while ( $child instanceof Frame_Decorator ) |
---|
194 | $child = $child->_frame; |
---|
195 | |
---|
196 | $this->_frame->append_child($child, $update_node); |
---|
197 | } |
---|
198 | |
---|
199 | function insert_child_before(Frame $new_child, Frame $ref, $update_node = true) { |
---|
200 | while ( $new_child instanceof Frame_Decorator ) |
---|
201 | $new_child = $new_child->_frame; |
---|
202 | |
---|
203 | if ( $ref instanceof Frame_Decorator ) |
---|
204 | $ref = $ref->_frame; |
---|
205 | |
---|
206 | $this->_frame->insert_child_before($new_child, $ref, $update_node); |
---|
207 | } |
---|
208 | |
---|
209 | function insert_child_after(Frame $new_child, Frame $ref, $update_node = true) { |
---|
210 | while ( $new_child instanceof Frame_Decorator ) |
---|
211 | $new_child = $new_child->_frame; |
---|
212 | |
---|
213 | while ( $ref instanceof Frame_Decorator ) |
---|
214 | $ref = $ref->_frame; |
---|
215 | |
---|
216 | $this->_frame->insert_child_after($new_child, $ref, $update_node); |
---|
217 | } |
---|
218 | |
---|
219 | function remove_child(Frame $child, $update_node = true) { |
---|
220 | while ( $child instanceof Frame_Decorator ) |
---|
221 | $child = $new_child->_frame; |
---|
222 | |
---|
223 | $this->_frame->remove_child($child, $update_node); |
---|
224 | } |
---|
225 | |
---|
226 | //........................................................................ |
---|
227 | |
---|
228 | function get_parent() { |
---|
229 | |
---|
230 | $p = $this->_frame->get_parent(); |
---|
231 | |
---|
232 | if ( $p && $deco = $p->get_decorator() ) { |
---|
233 | while ( $tmp = $deco->get_decorator() ) |
---|
234 | $deco = $tmp; |
---|
235 | return $deco; |
---|
236 | } else if ( $p ) |
---|
237 | return $p; |
---|
238 | else |
---|
239 | return null; |
---|
240 | } |
---|
241 | |
---|
242 | function get_first_child() { |
---|
243 | $c = $this->_frame->get_first_child(); |
---|
244 | if ( $c && $deco = $c->get_decorator() ) { |
---|
245 | while ( $tmp = $deco->get_decorator() ) |
---|
246 | $deco = $tmp; |
---|
247 | return $deco; |
---|
248 | } else if ( $c ) |
---|
249 | return $c; |
---|
250 | else |
---|
251 | return null; |
---|
252 | } |
---|
253 | |
---|
254 | function get_last_child() { |
---|
255 | $c = $this->_frame->get_last_child(); |
---|
256 | if ( $c && $deco = $c->get_decorator() ) { |
---|
257 | while ( $tmp = $deco->get_decorator() ) |
---|
258 | $deco = $tmp; |
---|
259 | return $deco; |
---|
260 | } else if ( $c ) |
---|
261 | return $c; |
---|
262 | else |
---|
263 | return null; |
---|
264 | } |
---|
265 | |
---|
266 | function get_prev_sibling() { |
---|
267 | $s = $this->_frame->get_prev_sibling(); |
---|
268 | if ( $s && $deco = $s->get_decorator() ) { |
---|
269 | while ( $tmp = $deco->get_decorator() ) |
---|
270 | $deco = $tmp; |
---|
271 | return $deco; |
---|
272 | } else if ( $s ) |
---|
273 | return $s; |
---|
274 | else |
---|
275 | return null; |
---|
276 | } |
---|
277 | |
---|
278 | function get_next_sibling() { |
---|
279 | $s = $this->_frame->get_next_sibling(); |
---|
280 | if ( $s && $deco = $s->get_decorator() ) { |
---|
281 | while ( $tmp = $deco->get_decorator() ) |
---|
282 | $deco = $tmp; |
---|
283 | return $deco; |
---|
284 | } else if ( $s ) |
---|
285 | return $s; |
---|
286 | else |
---|
287 | return null; |
---|
288 | } |
---|
289 | |
---|
290 | function get_children() { |
---|
291 | return new FrameList($this); |
---|
292 | } |
---|
293 | |
---|
294 | function get_subtree() { |
---|
295 | return new FrameTreeList($this); |
---|
296 | } |
---|
297 | |
---|
298 | //........................................................................ |
---|
299 | |
---|
300 | function set_positioner(Positioner $posn) { |
---|
301 | $this->_positioner = $posn; |
---|
302 | if ( $this->_frame instanceof Frame_Decorator ) |
---|
303 | $this->_frame->set_positioner($posn); |
---|
304 | } |
---|
305 | |
---|
306 | //........................................................................ |
---|
307 | |
---|
308 | function set_reflower(Frame_Reflower $reflower) { |
---|
309 | $this->_reflower = $reflower; |
---|
310 | if ( $this->_frame instanceof Frame_Decorator ) |
---|
311 | $this->_frame->set_reflower( $reflower ); |
---|
312 | } |
---|
313 | |
---|
314 | function get_reflower() { return $this->_reflower; } |
---|
315 | |
---|
316 | //........................................................................ |
---|
317 | |
---|
318 | function set_root(Frame $root) { |
---|
319 | $this->_root = $root; |
---|
320 | if ( $this->_frame instanceof Frame_Decorator ) |
---|
321 | $this->_frame->set_root($root); |
---|
322 | } |
---|
323 | |
---|
324 | function get_root() { return $this->_root; } |
---|
325 | |
---|
326 | //........................................................................ |
---|
327 | |
---|
328 | function find_block_parent() { |
---|
329 | |
---|
330 | // Find our nearest block level parent |
---|
331 | $p = $this->get_parent(); |
---|
332 | |
---|
333 | while ( $p ) { |
---|
334 | if ( in_array($p->get_style()->display, Style::$BLOCK_TYPES) ) |
---|
335 | break; |
---|
336 | |
---|
337 | $p = $p->get_parent(); |
---|
338 | } |
---|
339 | |
---|
340 | return $p; |
---|
341 | } |
---|
342 | |
---|
343 | //........................................................................ |
---|
344 | |
---|
345 | /** |
---|
346 | * split this frame at $child. |
---|
347 | * |
---|
348 | * The current frame is cloned and $child and all children following |
---|
349 | * $child are added to the clone. The clone is then passed to the |
---|
350 | * current frame's parent->split() method. |
---|
351 | * |
---|
352 | * @param Frame $child |
---|
353 | */ |
---|
354 | function split($child = null) { |
---|
355 | |
---|
356 | if ( is_null( $child ) ) { |
---|
357 | $this->get_parent()->split($this); |
---|
358 | return; |
---|
359 | } |
---|
360 | |
---|
361 | if ( $child->get_parent() !== $this ) |
---|
362 | throw new DOMPDF_Exception("Unable to split: frame is not a child of this one."); |
---|
363 | |
---|
364 | $split = $this->copy( $this->_frame->get_node()->cloneNode() ); |
---|
365 | $split->reset(); |
---|
366 | $this->get_parent()->insert_child_after($split, $this); |
---|
367 | |
---|
368 | // Add $frame and all following siblings to the new split node |
---|
369 | $iter = $child; |
---|
370 | while ($iter) { |
---|
371 | $frame = $iter; |
---|
372 | $iter = $iter->get_next_sibling(); |
---|
373 | $frame->reset(); |
---|
374 | $split->append_child($frame); |
---|
375 | } |
---|
376 | |
---|
377 | $this->get_parent()->split($split); |
---|
378 | } |
---|
379 | |
---|
380 | //........................................................................ |
---|
381 | |
---|
382 | final function position() { $this->_positioner->position(); } |
---|
383 | |
---|
384 | final function reflow() { |
---|
385 | // Uncomment this to see the frames before they're laid out, instead of |
---|
386 | // during rendering. |
---|
387 | //echo $this->_frame; flush(); |
---|
388 | $this->_reflower->reflow(); |
---|
389 | } |
---|
390 | |
---|
391 | final function get_min_max_width() { return $this->get_reflower()->get_min_max_width(); } |
---|
392 | |
---|
393 | //........................................................................ |
---|
394 | |
---|
395 | |
---|
396 | } |
---|
397 | |
---|
398 | ?> |
---|