1<?php
2// $Header: /cvsroot/html2ps/box.utils.text-align.inc.php,v 1.13 2007/01/09 20:13:48 Konstantin Exp $
3
4function ta_left(&$box, &$context, $lastline) {
5  // Do nothing; text is left-aligned by default
6}
7
8function ta_center(&$box, &$context, $lastline) {
9  $delta = $box->_line_length_delta($context) / 2;
10
11  $size = count($box->_line);
12  for ($i=0; $i< $size; $i++) {
13    $box->_line[$i]->offset($delta, 0);
14  };
15
16  $first_box =& $box->_line[0];
17  if (isset($first_box->wrapped) && !is_null($first_box->wrapped)) {
18    $first_box->offset_wrapped(-$delta, 0);
19  };
20}
21
22function ta_right(&$box, &$context, $lastline) {
23  $delta = $box->_line_length_delta($context);
24
25  $size = count($box->_line);
26  for ($i=0; $i<$size; $i++) {
27    $box->_line[$i]->offset($delta, 0);
28  };
29
30  $first_box =& $box->_line[0];
31  if (isset($first_box->wrapped) && !is_null($first_box->wrapped)) {
32    $first_box->offset_wrapped(-$delta, 0);
33  };
34}
35
36function ta_justify(&$box, &$context, $lastline) {
37  // last line is never justified
38  if ($lastline) {
39    return;
40  }
41
42  // If line box contains less that two items, no justification can be done, just return
43  if (count($box->_line) < 2) {
44    return;
45  }
46
47  // Calculate extra space to be filled by this line
48  $delta = $box->_line_length_delta($context);
49
50  // note that if it is the very first line inside the container, 'text-indent' value
51  // should not be taken into account while calculating delta value
52  if (count($box->content) > 0) {
53    if ($box->content[0]->uid === $box->_line[0]->uid) {
54      $delta -= $box->text_indent->calculate($box);
55    };
56  };
57
58  // if line takes less that MAX_JUSTIFY_FRACTION of available space, no justtification should be done
59  if ($delta > $box->_line_length() * MAX_JUSTIFY_FRACTION) {
60    return;
61  };
62
63  // Calculate offset for each whitespace box
64  $whitespace_count = 0;
65  $size = count($box->_line);
66
67  // Why $size-1? Ignore whitespace box, if it is located at the very end of
68  // line box
69
70  // Also, ignore whitespace box at the very beginning of the line
71  for ($i=1; $i<$size-1; $i++) {
72    if (is_a($box->_line[$i],"WhitespaceBox")) {
73      $whitespace_count++;
74    };
75  };
76
77  if ($whitespace_count > 0) {
78    $offset = $delta / $whitespace_count;
79  } else {
80    $offset = 0;
81  };
82
83  // Offset all boxes in current line box
84  $num_whitespaces = 0;
85  $size = count($box->_line);
86  for ($i=1; $i < $size; $i++) {
87    /*
88     * Note that order is important: additional horizontal space
89     * is added after the whitespace box; it is important, as
90     * whitespace box (if it is the last box in the line) should not
91     * run off the right edge of the container box
92     */
93    $box->_line[$i]->offset($offset * $num_whitespaces, 0);
94
95    if (is_a($box->_line[$i],"WhitespaceBox")) {
96      $num_whitespaces++;
97    };
98  };
99
100  // The very first box is not offset in this case, so we don't need to
101  // call offset_wrapped to compensate this.
102}
103?>