File size: 10,134 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
// $Header: /cvsroot/html2ps/box.table.cell.php,v 1.40 2007/01/24 18:55:45 Konstantin Exp $

class TableCellBox extends GenericContainerBox {
  var $colspan;
  var $rowspan;
  var $column;

  var $_suppress_first;
  var $_suppress_last;

  function get_min_width(&$context) {   
    if (isset($this->_cache[CACHE_MIN_WIDTH])) {
      return $this->_cache[CACHE_MIN_WIDTH];
    };

    $content_size = count($this->content);

    /**
     * If box does not have any context, its minimal width is determined by extra horizontal space:
     * padding, border width and margins
     */
    if ($content_size == 0) { 
      $min_width = $this->_get_hor_extra();
      $this->_cache[CACHE_MIN_WIDTH] = $min_width;
      return $min_width;
    };

    /**
     * If we're in 'nowrap' mode, minimal and maximal width will be equal
     */
    $white_space = $this->getCSSProperty(CSS_WHITE_SPACE);
    $pseudo_nowrap = $this->getCSSProperty(CSS_HTML2PS_NOWRAP);
    if ($white_space   == WHITESPACE_NOWRAP || 
        $pseudo_nowrap == NOWRAP_NOWRAP) { 
      $min_width = $this->get_min_nowrap_width($context);
      $this->_cache[CACHE_MIN_WIDTH] = $min_width;
      return $min_width; 
    }

    /**
     * We need to add text indent size to the with of the first item
     */
    $start_index = 0;
    while ($start_index < $content_size && 
           $this->content[$start_index]->out_of_flow()) {
      $start_index++; 
    };
    
    if ($start_index < $content_size) {
      $ti = $this->getCSSProperty(CSS_TEXT_INDENT);
      $minw = 
        $ti->calculate($this) + 
        $this->content[$start_index]->get_min_width($context);
    } else {
      $minw = 0;
    };

    for ($i=$start_index; $i<$content_size; $i++) {
      $item =& $this->content[$i];
      if (!$item->out_of_flow()) {
        $minw = max($minw, $item->get_min_width_natural($context));
      };
    }

    /**
     * Apply width constraint to min width. Return maximal value
     */
    $wc = $this->getCSSProperty(CSS_WIDTH);
    $min_width = max($minw, 
                     $wc->apply($minw, $this->parent->get_width())) + $this->_get_hor_extra();
    $this->_cache[CACHE_MIN_WIDTH] = $min_width;
    return $min_width;
  }

  function readCSS(&$state) {
    parent::readCSS($state);

    $this->_readCSS($state,
                    array(CSS_BORDER_COLLAPSE));

    $this->_readCSSLengths($state,
                           array(CSS_HTML2PS_CELLPADDING,
                                 CSS_HTML2PS_CELLSPACING,
                                 CSS_HTML2PS_TABLE_BORDER));
  }

  function isCell() { 
    return true;
  }

  function is_fake() {
    return false;
  }

  function &create(&$root, &$pipeline) {
    $css_state = $pipeline->getCurrentCSSState();

    $box =& new TableCellBox();
    $box->readCSS($css_state);

    // Use cellspacing / cellpadding values from the containing table
    $cellspacing = $box->getCSSProperty(CSS_HTML2PS_CELLSPACING);
    $cellpadding = $box->getCSSProperty(CSS_HTML2PS_CELLPADDING);

    // FIXME: I'll need to resolve that issue with COLLAPSING border model. Now borders
    // are rendered separated

    // if not border set explicitly, inherit value set via border attribute of TABLE tag
    $border_handler = CSS::get_handler(CSS_BORDER);
    if ($border_handler->is_default($box->getCSSProperty(CSS_BORDER))) {
      $table_border = $box->getCSSProperty(CSS_HTML2PS_TABLE_BORDER);
      $box->setCSSProperty(CSS_BORDER, $table_border);
    };

    $margin =& CSS::get_handler(CSS_MARGIN);
    $box->setCSSProperty(CSS_MARGIN, $margin->default_value());
      
    $h_padding =& CSS::get_handler(CSS_PADDING);
    $padding = $box->getCSSProperty(CSS_PADDING);

    if ($h_padding->is_default($padding)) {
      $padding->left->_units       = $cellpadding;
      $padding->left->auto         = false;
      $padding->left->percentage   = null;

      $padding->right->_units      = $cellpadding;
      $padding->right->auto        = false;
      $padding->right->percentage  = null;

      $padding->top->_units        = $cellpadding;
      $padding->top->auto          = false;
      $padding->top->percentage    = null;

      $padding->bottom->_units     = $cellpadding;
      $padding->bottom->auto       = false;
      $padding->bottom->percentage = null;

      /**
       * Note that cellpadding/cellspacing values never use font-size based units
       * ('em' and 'ex'), so we may pass 0 as base_font_size parameter - it 
       * will not be used anyway
       */
      $padding->units2pt(0);

      $box->setCSSProperty(CSS_PADDING, $padding);
    };
       
    if ($box->getCSSProperty(CSS_BORDER_COLLAPSE) != BORDER_COLLAPSE) {
      $margin_value = $box->getCSSProperty(CSS_MARGIN);
      if ($margin->is_default($margin_value)) {
        $length = $cellspacing->copy();
        $length->scale(0.5);

        $margin_value->left->_units       = $length;
        $margin_value->left->auto         = false;
        $margin_value->left->percentage   = null;

        $margin_value->right->_units      = $length;
        $margin_value->right->auto        = false;
        $margin_value->right->percentage  = null;

        $margin_value->top->_units        = $length;
        $margin_value->top->auto          = false;
        $margin_value->top->percentage    = null;

        $margin_value->bottom->_units     = $length;
        $margin_value->bottom->auto       = false;
        $margin_value->bottom->percentage = null;

        /**
         * Note that cellpadding/cellspacing values never use font-size based units
         * ('em' and 'ex'), so we may pass 0 as base_font_size parameter - it 
         * will not be used anyway
         */
        $margin_value->units2pt(0);

        $box->setCSSProperty(CSS_MARGIN, $margin_value);
      }
    };

    // Save colspan and rowspan information
    $box->colspan = max(1,(int)$root->get_attribute('colspan'));
    $box->rowspan = max(1,(int)$root->get_attribute('rowspan'));

    // Create content 

    // 'vertical-align' CSS value is not inherited from the table cells
    $css_state->pushState();

    $handler =& CSS::get_handler(CSS_VERTICAL_ALIGN);
    $handler->replace($handler->default_value(),
                      $css_state);

    $box->create_content($root, $pipeline);

    global $g_config;
    if ($g_config['mode'] == "quirks") {
      // QUIRKS MODE:
      // H1-H6 and P elements should have their top/bottom margin suppressed if they occur as the first/last table cell child 
      // correspondingly; note that we cannot do it usung CSS rules, as there's no selectors for the last child. 
      //
      $child = $root->first_child();
      if ($child) {
        while ($child && $child->node_type() != XML_ELEMENT_NODE) {
          $child = $child->next_sibling();
        };
      
        if ($child) {
          if (array_search(strtolower($child->tagname()), array("h1","h2","h3","h4","h5","h6","p"))) {
            $box->_suppress_first = true;
          }
        };
      };

      $child = $root->last_child();
      if ($child) {
        while ($child && $child->node_type() != XML_ELEMENT_NODE) {
          $child = $child->previous_sibling();
        };
        
        if ($child) {
          if (array_search(strtolower($child->tagname()), array("h1","h2","h3","h4","h5","h6","p"))) {
            $box->_suppress_last = true;
          }
        };
      };
    };

    // pop the default vertical-align value
    $css_state->popState();

    return $box;
  }

  function TableCellBox() {
    // Call parent constructor
    $this->GenericContainerBox();

    $this->_suppress_first = false;
    $this->_suppress_last  = false;
    
    $this->colspan = 1;
    $this->rowspan = 1;

    // This value will be overwritten in table 'normalize_parent' method
    //
    $this->column  = 0;
    $this->row     = 0;
  }

  // Inherited from GenericFormattedBox

  function get_cell_baseline() {
    $content = $this->get_first_data();
    if (is_null($content)) { 
      return 0; 
    }
    return $content->baseline;
  }

  // Flow-control
  function reflow(&$parent, &$context) {
    GenericFormattedBox::reflow($parent, $context);

    global $g_config;
    $size = count($this->content);
    if ($g_config['mode'] == "quirks" && $size > 0) {
      // QUIRKS MODE:
      // H1-H6 and P elements should have their top/bottom margin suppressed if they occur as the first/last table cell child 
      // correspondingly; note that we cannot do it usung CSS rules, as there's no selectors for the last child. 
      //
      
      $first =& $this->get_first();
      if (!is_null($first) && $this->_suppress_first && $first->isBlockLevel()) {
        $first->margin->top->value = 0;
        $first->margin->top->percentage = null;
      };

      $last =& $this->get_last();
      if (!is_null($last) && $this->_suppress_last && $last->isBlockLevel()) {
        $last->margin->bottom->value = 0;
        $last->margin->bottom->percentage = null;
      };
    };

    // Determine upper-left _content_ corner position of current box 
    $this->put_left($parent->_current_x + $this->get_extra_left());

    // NOTE: Table cell margin is used as a cell-spacing value
    $border = $this->getCSSProperty(CSS_BORDER);
    $padding = $this->getCSSProperty(CSS_PADDING);
    $this->put_top($parent->_current_y - 
                   $border->top->get_width() - 
                   $padding->top->value);

    // CSS 2.1: 
    // Floats, absolutely positioned elements, inline-blocks, table-cells, and elements with 'overflow' other than
    // 'visible' establish new block formatting contexts.
    $context->push();
    $context->push_container_uid($this->uid);

    // Reflow cell content
    $this->reflow_content($context);

    // Extend the table cell height to fit all contained floats
    //
    // Determine the bottom edge corrdinate of the bottommost float
    //
    $float_bottom = $context->float_bottom();
      
    if (!is_null($float_bottom)) {
      $this->extend_height($float_bottom);
    };

    // Restore old context
    $context->pop_container_uid();
    $context->pop();
  }
}

?>