source: trunk/filemanager/tp/dompdf/include/table_frame_reflower.cls.php @ 2000

Revision 2000, 17.2 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implementação do módulo gerenciador de arquivos

Line 
1<?php
2/**
3 * DOMPDF - PHP5 HTML to PDF renderer
4 *
5 * File: $RCSfile: table_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: table_frame_reflower.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */
41
42/**
43 * Reflows tables
44 *
45 * @access private
46 * @package dompdf
47 */
48class Table_Frame_Reflower extends Frame_Reflower {
49
50  /**
51   * Cache of results between call to get_min_max_width and assign_widths
52   *
53   * @var array
54   */
55  protected $_state;
56
57  function __construct(Table_Frame_Decorator $frame) {
58    $this->_state = null;
59    parent::__construct($frame);
60  }
61
62  /**
63   * State is held here so it needs to be reset along with the decorator
64   */
65  function reset() {
66    $this->_state = null;
67    $this->_min_max_cache = null;
68  }
69
70  //........................................................................
71
72  protected function _assign_widths() {
73    $style = $this->_frame->get_style();
74
75    // Find the min/max width of the table and sort the columns into
76    // absolute/percent/auto arrays
77    $min_width = $this->_state["min_width"];
78    $max_width = $this->_state["max_width"];
79    $percent_used = $this->_state["percent_used"];
80    $absolute_used = $this->_state["absolute_used"];
81    $auto_min = $this->_state["auto_min"];
82
83    $absolute =& $this->_state["absolute"];
84    $percent =& $this->_state["percent"];
85    $auto =& $this->_state["auto"];
86
87    // Determine the actual width of the table
88    $cb = $this->_frame->get_containing_block();
89    $columns =& $this->_frame->get_cellmap()->get_columns();
90
91    $width = $style->width;
92
93    // Calcluate padding & border fudge factor
94    $left = $style->margin_left;
95    $right = $style->margin_right;
96
97    $left = $left == "auto" ? 0 : $style->length_in_pt($left, $cb["w"]);
98    $right = $right == "auto" ? 0 : $style->length_in_pt($right, $cb["w"]);
99
100    $delta = $left + $right + $style->length_in_pt(array($style->padding_left,
101                                                         $style->border_left_width,
102                                                         $style->border_right_width,
103                                                         $style->padding_right), $cb["w"]);
104
105    $min_table_width = $style->length_in_pt( $style->min_width, $cb["w"] - $delta );
106
107    // min & max widths already include borders & padding
108    $min_width -= $delta;
109    $max_width -= $delta;
110   
111    if ( $width !== "auto" ) {
112
113      $preferred_width = $style->length_in_pt($width, $cb["w"]) - $delta;
114
115      if ( $preferred_width < $min_table_width )
116        $preferred_width = $min_table_width;
117
118      if ( $preferred_width > $min_width )
119        $width = $preferred_width;
120      else
121        $width = $min_width;
122
123    } else {
124
125      if ( $max_width + $delta < $cb["w"] )
126        $width = $max_width;
127      else if ( $cb["w"] - $delta > $min_width )
128        $width = $cb["w"] - $delta;
129      else
130        $width = $min_width;
131
132      if ( $width < $min_table_width )
133        $width = $min_table_width;
134
135    }
136
137    // Store our resolved width
138    $style->width = $width;
139
140    $cellmap = $this->_frame->get_cellmap();
141
142    // If the whole table fits on the page, then assign each column it's max width
143    if ( $width == $max_width ) {
144
145      foreach (array_keys($columns) as $i)
146        $cellmap->set_column_width($i, $columns[$i]["max-width"]);
147
148      return;
149    }
150
151    // Determine leftover and assign it evenly to all columns
152    if ( $width > $min_width ) {
153
154      // We have four cases to deal with:
155      //
156      // 1. All columns are auto--no widths have been specified.  In this
157      // case we distribute extra space across all columns weighted by max-width.
158      //
159      // 2. Only absolute widths have been specified.  In this case we
160      // distribute any extra space equally among 'width: auto' columns, or all
161      // columns if no auto columns have been specified.
162      //
163      // 3. Only percentage widths have been specified.  In this case we
164      // normalize the percentage values and distribute any remaining % to
165      // width: auto columns.  We then proceed to assign widths as fractions
166      // of the table width.
167      //
168      // 4. Both absolute and percentage widths have been specified.
169
170      // Case 1:
171      if ( $absolute_used == 0 && $percent_used == 0 ) {
172        $increment = $width - $min_width;
173
174        foreach (array_keys($columns) as $i)
175          $cellmap->set_column_width($i, $columns[$i]["min-width"] + $increment * ($columns[$i]["max-width"] / $max_width));
176        return;
177      }
178
179
180      // Case 2
181      if ( $absolute_used > 0 && $percent_used == 0 ) {
182
183        if ( count($auto) > 0 )
184          $increment = ($width - $auto_min - $absolute_used) / count($auto);
185
186        // Use the absolutely specified width or the increment
187        foreach (array_keys($columns) as $i) {
188
189          if ( $columns[$i]["absolute"] > 0 && count($auto) )
190            $cellmap->set_column_width($i, $columns[$i]["min-width"]);
191          else if ( count($auto) )
192            $cellmap->set_column_width($i, $columns[$i]["min-width"] + $increment);
193          else {
194            // All absolute columns
195            $increment = ($width - $absolute_used) * $columns[$i]["absolute"] / $absolute_used;
196
197            $cellmap->set_column_width($i, $columns[$i]["min-width"] + $increment);
198          }
199
200        }
201        return;
202      }
203
204
205      // Case 3:
206      if ( $absolute_used == 0 && $percent_used > 0 ) {
207
208        $scale = null;
209        $remaining = null;
210
211        // Scale percent values if the total percentage is > 100, or if all
212        // values are specified as percentages.
213        if ( $percent_used > 100 || count($auto) == 0)
214          $scale = 100 / $percent_used;
215        else
216          $scale = 1;
217
218        // Account for the minimum space used by the unassigned auto columns
219        $used_width = $auto_min;
220
221        foreach ($percent as $i) {
222          $columns[$i]["percent"] *= $scale;
223
224          $slack = $width - $used_width;
225
226          $w = min($columns[$i]["percent"] * $width/100, $slack);
227
228          if ( $w < $columns[$i]["min-width"] )
229            $w = $columns[$i]["min-width"];
230
231          $cellmap->set_column_width($i, $w);
232          $used_width += $w;
233
234        }
235
236        // This works because $used_width includes the min-width of each
237        // unassigned column
238        if ( count($auto) > 0 ) {
239          $increment = ($width - $used_width) / count($auto);
240
241          foreach ($auto as $i)
242            $cellmap->set_column_width($i, $columns[$i]["min-width"] + $increment);
243
244        }
245        return;
246      }
247
248      // Case 4:
249
250      // First-come, first served
251      if ( $absolute_used > 0 && $percent_used > 0 ) {
252
253        $used_width = $auto_min;
254
255        foreach ($absolute as $i) {
256          $cellmap->set_column_width($i, $columns[$i]["min-width"]);
257          $used_width +=  $columns[$i]["min-width"];
258        }
259
260        // Scale percent values if the total percentage is > 100 or there
261        // are no auto values to take up slack
262        if ( $percent_used > 100 || count($auto) == 0 )
263          $scale = 100 / $percent_used;
264        else
265          $scale = 1;
266
267        $remaining_width = $width - $used_width;
268
269        foreach ($percent as $i) {
270          $slack = $remaining_width - $used_width;
271
272          $columns[$i]["percent"] *= $scale;
273          $w = min($columns[$i]["percent"] * $remaining_width / 100, $slack);
274
275          if ( $w < $columns[$i]["min-width"] )
276            $w = $columns[$i]["min-width"];
277
278          $columns[$i]["used-width"] = $w;
279          $used_width += $w;
280        }
281
282        if ( count($auto) > 0 ) {
283          $increment = ($width - $used_width) / count($auto);
284
285          foreach ($auto as $i)
286            $cellmap->set_column_width($i, $columns[$i]["min-width"] + $increment);
287
288        }
289
290        return;
291      }
292
293
294    } else { // we are over constrained
295
296      // Each column gets its minimum width
297      foreach (array_keys($columns) as $i)
298        $cellmap->set_column_width($i, $columns[$i]["min-width"]);
299
300    }
301  }
302
303  //........................................................................
304
305  // Determine the frame's height based on min/max height
306  protected function _calculate_height() {
307
308    $style = $this->_frame->get_style();
309    $height = $style->height;
310
311    $cellmap = $this->_frame->get_cellmap();
312    $cellmap->assign_frame_heights();
313    $rows = $cellmap->get_rows();
314
315    // Determine our content height
316    $content_height = 0;
317    foreach ( $rows as $r )
318      $content_height += $r["height"];
319
320    $cb = $this->_frame->get_containing_block();
321
322    if ( !($style->overflow === "visible" ||
323           ($style->overflow === "hidden" && $height === "auto")) ) {
324
325      // Only handle min/max height if the height is independent of the frame's content
326
327      $min_height = $style->min_height;
328      $max_height = $style->max_height;
329
330      if ( isset($cb["h"]) ) {
331        $min_height = $style->length_in_pt($min_height, $cb["h"]);
332        $max_height = $style->length_in_pt($max_height, $cb["h"]);
333
334      } else if ( isset($cb["w"]) ) {
335
336        if ( mb_strpos($min_height, "%") !== false )
337          $min_height = 0;
338        else
339          $min_height = $style->length_in_pt($min_height, $cb["w"]);
340
341        if ( mb_strpos($max_height, "%") !== false )
342          $max_height = "none";
343        else
344          $max_height = $style->length_in_pt($max_height, $cb["w"]);
345      }
346
347      if ( $max_height !== "none" && $min_height > $max_height )
348        // Swap 'em
349        list($max_height, $min_height) = array($min_height, $max_height);
350
351      if ( $max_height !== "none" && $height > $max_height )
352        $height = $max_height;
353
354      if ( $height < $min_height )
355        $height = $min_height;
356
357    } else {
358
359      // Use the content height or the height value, whichever is greater
360      if ( $height !== "auto" ) {
361        $height = $style->length_in_pt($height, $cb["h"]);
362
363        if ( $height <= $content_height )
364          $height = $content_height;
365        else
366          $cellmap->set_frame_heights($height,$content_height);
367
368      } else
369        $height = $content_height;
370
371    }
372
373    return $height;
374
375  }
376  //........................................................................
377
378  function reflow() {
379
380    // Check if a page break is forced
381    $page = $this->_frame->get_root();
382    $page->check_forced_page_break($this->_frame);
383
384    // Bail if the page is full
385    if ( $page->is_full() )
386      return;
387   
388    // Let the page know that we're reflowing a table so that splits
389    // are suppressed (simply setting page-break-inside: avoid won't
390    // work because we may have an arbitrary number of block elements
391    // inside tds.)
392    $page->table_reflow_start();
393
394   
395    // Collapse vertical margins, if required
396    $this->_collapse_margins();
397
398    $this->_frame->position();
399
400    // Table layout algorithm:
401    // http://www.w3.org/TR/CSS21/tables.html#auto-table-layout
402
403    if ( is_null($this->_state) )
404      $this->get_min_max_width();
405
406    $cb = $this->_frame->get_containing_block();
407    $style = $this->_frame->get_style();
408
409    // This is slightly inexact, but should be okay.  Add half the
410    // border-spacing to the table as padding.  The other half is added to
411    // the cells themselves.
412    if ( $style->border_collapse === "separate" ) {
413      list($h, $v) = $style->border_spacing;
414
415      $v = $style->length_in_pt($v) / 2;
416      $h = $style->length_in_pt($h) / 2;
417
418      $style->padding_left = $style->length_in_pt($style->padding_left, $cb["w"]) + $h;
419      $style->padding_right = $style->length_in_pt($style->padding_right, $cb["w"]) + $h;
420      $style->padding_top = $style->length_in_pt($style->padding_top, $cb["w"]) + $v;
421      $style->padding_bottom = $style->length_in_pt($style->padding_bottom, $cb["w"]) + $v;
422
423    }
424
425    $this->_assign_widths();
426
427    // Adjust left & right margins, if they are auto
428    $width = $style->width;
429    $left = $style->margin_left;
430    $right = $style->margin_right;
431
432    $diff = $cb["w"] - $width;
433
434    if ( $left === "auto" && $right === "auto" && $diff > 0 ) {
435      $left = $right = $diff / 2;
436      $style->margin_left = "$left pt";
437      $style->margin_right = "$right pt";
438
439    } else {
440      $left = $style->length_in_pt($left, $cb["w"]);
441      $right = $style->length_in_pt($right, $cb["w"]);
442    }
443
444
445    list($x, $y) = $this->_frame->get_position();
446
447    // Determine the content edge
448    $content_x = $x + $left + $style->length_in_pt(array($style->padding_left,
449                                                         $style->border_left_width), $cb["w"]);
450    $content_y = $y + $style->length_in_pt(array($style->margin_top,
451                                                 $style->border_top_width,
452                                                 $style->padding_top), $cb["w"]);
453
454    if ( isset($cb["h"]) )
455      $h = $cb["h"];
456    else
457      $h = null;
458
459
460    $cellmap = $this->_frame->get_cellmap();
461    $col =& $cellmap->get_column(0);
462    $col["x"] = $content_x;
463
464    $row =& $cellmap->get_row(0);
465    $row["y"] = $content_y;
466
467    $cellmap->assign_x_positions();
468
469    // Set the containing block of each child & reflow
470    foreach ( $this->_frame->get_children() as $child ) {
471
472      // Bail if the page is full
473      if ( !$page->in_nested_table() && $page->is_full() )
474        break;
475
476      $child->set_containing_block($content_x, $content_y, $width, $h);
477      $child->reflow();
478
479      if ( !$page->in_nested_table() )
480        // Check if a split has occured
481        $page->check_page_break($child);
482
483    }
484
485    // Assign heights to our cells:
486    $style->height = $this->_calculate_height();
487
488    if ( $style->border_collapse === "collapse" ) {
489      // Unset our borders because our cells are now using them
490      $style->border_style = "none";
491    }
492
493    $page->table_reflow_end();
494
495    // Debugging:
496    //echo ($this->_frame->get_cellmap());
497  }
498
499  //........................................................................
500
501  function get_min_max_width() {
502
503    if ( !is_null($this->_min_max_cache)  )
504      return $this->_min_max_cache;
505
506    $style = $this->_frame->get_style();
507
508    $this->_frame->normalise();
509
510    // Add the cells to the cellmap (this will calcluate column widths as
511    // frames are added)
512    $this->_frame->get_cellmap()->add_frame($this->_frame);
513
514    // Find the min/max width of the table and sort the columns into
515    // absolute/percent/auto arrays
516    $this->_state = array();
517    $this->_state["min_width"] = 0;
518    $this->_state["max_width"] = 0;
519
520    $this->_state["percent_used"] = 0;
521    $this->_state["absolute_used"] = 0;
522    $this->_state["auto_min"] = 0;
523
524    $this->_state["absolute"] = array();
525    $this->_state["percent"] = array();
526    $this->_state["auto"] = array();
527
528    $columns =& $this->_frame->get_cellmap()->get_columns();
529    foreach (array_keys($columns) as $i) {
530      $this->_state["min_width"] += $columns[$i]["min-width"];
531      $this->_state["max_width"] += $columns[$i]["max-width"];
532
533      if ( $columns[$i]["absolute"] > 0 ) {
534        $this->_state["absolute"][] = $i;
535        $this->_state["absolute_used"] += $columns[$i]["absolute"];
536
537      } else if ( $columns[$i]["percent"] > 0 ) {
538        $this->_state["percent"][] = $i;
539        $this->_state["percent_used"] += $columns[$i]["percent"];
540
541      } else {
542        $this->_state["auto"][] = $i;
543        $this->_state["auto_min"] += $columns[$i]["min-width"];
544      }
545    }
546
547    // Account for margins & padding
548    $dims = array($style->border_left_width,
549                  $style->border_right_width,
550                  $style->padding_left,
551                  $style->padding_right,
552                  $style->margin_left,
553                  $style->margin_right);
554
555    if ( $style->border_collapse != "collapse" )
556      list($dims[]) = $style->border_spacing;
557
558    $delta = $style->length_in_pt($dims, $this->_frame->get_containing_block("w"));
559
560    $this->_state["min_width"] += $delta;
561    $this->_state["max_width"] += $delta;
562
563    return $this->_min_max_cache = array($this->_state["min_width"], $this->_state["max_width"],
564                 "min" => $this->_state["min_width"], "max" => $this->_state["max_width"]);
565  }
566}
567
568?>
Note: See TracBrowser for help on using the repository browser.