1<?php
2
3class StrategyWidthMaxNatural {
4  var $_limit;
5  var $_maxw;
6  var $_cmaxw;
7
8  function StrategyWidthMaxNatural($limit = 10E6) {
9    $this->_limit = $limit;
10  }
11
12  function add_width($delta) {
13    if ($this->_cmaxw + $delta > $this->_limit) {
14      $this->line_break();
15    };
16    $this->_cmaxw += $delta;
17  }
18
19  function line_break() {
20    $this->_maxw  = max($this->_maxw, $this->_cmaxw);
21    $this->_cmaxw = 0;
22  }
23
24  function apply(&$box, &$context) {
25    $this->_maxw = 0;
26
27    // We need to add text indent to the max width
28    $text_indent = $box->get_css_property(CSS_TEXT_INDENT);
29    $this->_cmaxw = $text_indent->calculate($box);
30
31    for ($i=0, $size = count($box->content); $i<$size; $i++) {
32      $child =& $box->content[$i];
33
34      // Note that while BR-generated box is out of flow,
35      // it should break the current line
36      if ($child->isLineBreak()) {
37        $this->line_break();
38
39      } elseif (!$child->out_of_flow()) {
40        if (is_inline($child)) {
41          $this->add_width($child->get_max_width_natural($context, $this->_limit));
42
43        } elseif ($child->get_css_property(CSS_FLOAT) !== FLOAT_NONE) {
44          $wc = $child->get_css_property(CSS_WIDTH);
45
46          if (!$wc->isFraction()) {
47            $delta = $child->get_max_width($context, $this->_limit);
48          } else {
49            $delta = $child->get_max_width_natural($context, $this->_limit);
50          };
51
52          $this->add_width($delta);
53        } else {
54          $this->_maxw  = max($this->_maxw, $this->_cmaxw);
55          $this->_cmaxw = $child->get_max_width_natural($context, $this->_limit);
56
57          // Process special case with percentage constrained table
58          $item = $child;
59          $item_wc = $item->get_css_property(CSS_WIDTH);
60
61          if (is_a($item, "TableBox") &&
62              $item_wc->isFraction()) {
63            if (isset($child->parent) && $child->parent) {
64              $this->_cmaxw = max($this->_cmaxw,
65                                  $item_wc->apply($child->get_width(),
66                                                  $child->parent->get_expandable_width()));
67            } else {
68              $this->_cmaxw = max($this->_cmaxw,
69                                  $item_wc->apply($child->get_width(),
70                                                  $child->get_width()));
71            };
72          };
73
74          $this->line_break();
75        };
76      };
77    }
78
79    // Check if last line have maximal width
80    //
81    $this->_maxw = max($this->_maxw, $this->_cmaxw);
82
83    return $this->_maxw + $box->_get_hor_extra();
84  }
85}
86
87?>