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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
<?php
// $Header: /cvsroot/html2ps/box.container.php,v 1.67 2007/01/10 03:40:28 Konstantin Exp $

require_once(HTML2PS_DIR.'strategy.width.min.php');
require_once(HTML2PS_DIR.'strategy.width.min.nowrap.php');
require_once(HTML2PS_DIR.'strategy.width.max.php');
require_once(HTML2PS_DIR.'strategy.width.max.natural.php');

/**
 * @package HTML2PS
 * @subpackage Document
 *
 * This file contains the abstract class describing the behavior of document element
 * containing some other document elements.
 */

/**
 * @package HTML2PS
 * @subpackage Document
 * 
 * The GenericContainerBox class is a common superclass for all document elements able 
 * to contain other elements. This class does provide the line-box handling utilies and
 * some minor float related-functions.
 * 
 */
class GenericContainerBox extends GenericFormattedBox {
  /**
   * @var Array A list of contained elements (of type GenericFormattedBox)
   * @access public
   */
  var $content;

  var $_first_line;

  /**
   * @var Array A list of child nodes in the current line box; changes dynamically 
   * during the reflow process.
   * @access private
   */
  var $_line;

  /**
   * Sometimes floats may appear inside the line box, consider the following code,
   * for example: "<div>text<div style='float:left'>float</div>word</div>". In
   * this case, the floating DIV should be rendered below the "text word" line;
   * thus, we need to keep a list of deferred floating elements and render them
   * when current line box closes.
   *
   * @var Array A list of floats which should be flown after current line box ends;
   * @access private
   */
  var $_deferred_floats;

  /**
   * @var float Current output X value inside the current element
   * @access public
   */
  var $_current_x;

  /**
   * @var float Current output Y value inside the current element
   * @access public
   */
  var $_current_y;

  function destroy() {
    for ($i=0, $size = count($this->content); $i < $size; $i++) {
      $this->content[$i]->destroy();
    };
    unset($this->content);

    parent::destroy();
  }

  /** 
   * Render current container box using the specified output method.
   *
   * @param OutputDriver $driver The output driver object
   * 
   * @return Boolean flag indicating the success or 'null' value in case of critical rendering 
   * error
   */
  function show(&$driver) {
    GenericFormattedBox::show($driver);

    $overflow = $this->getCSSProperty(CSS_OVERFLOW);

    /**
     * Sometimes the content may overflow container boxes. This situation arise, for example,
     * for relative-positioned child boxes, boxes having constrained height and in some
     * other cases. If the container box does not have CSS 'overflow' property 
     * set to 'visible' value, the content should be visually clipped using container box
     * padding area.
     */
    if ($overflow !== OVERFLOW_VISIBLE) {
      $driver->save();
      $this->_setupClip($driver);
    };    

    /**
     * Render child elements
     */
    for ($i=0, $size = count($this->content); $i < $size; $i++) {    
      $child =& $this->content[$i];

      /**
       * We'll check the visibility property here
       * Reason: all boxes (except the top-level one) are contained in some other box, 
       * so every box will pass this check. The alternative is to add this check into every
       * box class show member.
       *
       * The only exception of absolute positioned block boxes which are drawn separately;
       * their show method is called explicitly; the similar check should be performed there
       */
      if ($child->isVisibleInFlow()) {
        /**
         * To reduce the drawing overhead, we'll check if some part if current child element
         * belongs to current output page. If not, there will be no reason to draw this 
         * child this time.
         * 
         * @see OutputDriver::contains()
         *
         * @todo In rare cases the element content may be placed outside the element itself;
         * in such situantion content may be visible on the page, while element is not.
         * This situation should be resolved somehow.
         */
        if ($driver->contains($child)) {
          if (is_null($child->show($driver))) {
            return null;
          };
        };
      };
    }

    /** 
     * Restore previous clipping mode, if it have been modified for non-'overflow: visible' 
     * box.
     */
    if ($overflow !== OVERFLOW_VISIBLE) {
      $driver->restore();
    };

    return true;
  }

  /** 
   * Render current fixed-positioned container box using the specified output method. Unlike
   * the 'show' method, there's no check if current page viewport contains current element, as
   * fixed-positioned may be drawn on the page margins, outside the viewport.
   *
   * @param OutputDriver $driver The output driver object
   * 
   * @return Boolean flag indicating the success or 'null' value in case of critical rendering 
   * error
   *
   * @see GenericContainerBox::show()
   * 
   * @todo the 'show' and 'show_fixed' method code are almost the same except the child element 
   * method called in the inner loop; also, no check is done if current viewport contains this element,
   * thus sllowinf printing data on page margins, where no data should be printed normally
   * I suppose some more generic method containing the common code should be made.
   */
  function show_fixed(&$driver) {
    GenericFormattedBox::show($driver);

    $overflow = $this->getCSSProperty(CSS_OVERFLOW);

    /**
     * Sometimes the content may overflow container boxes. This situation arise, for example,
     * for relative-positioned child boxes, boxes having constrained height and in some
     * other cases. If the container box does not have CSS 'overflow' property 
     * set to 'visible' value, the content should be visually clipped using container box
     * padding area.
     */
    if ($overflow !== OVERFLOW_VISIBLE) {
      // Save graphics state (of course, BEFORE the clipping area will be set)
      $driver->save();
      $this->_setupClip($driver);
    };    

    /**
     * Render child elements
     */
    $size = count($this->content);
    for ($i=0; $i < $size; $i++) {    
      /**
       * We'll check the visibility property here
       * Reason: all boxes (except the top-level one) are contained in some other box, 
       * so every box will pass this check. The alternative is to add this check into every
       * box class show member.
       *
       * The only exception of absolute positioned block boxes which are drawn separately;
       * their show method is called explicitly; the similar check should be performed there
       */
      $child =& $this->content[$i];
      if ($child->getCSSProperty(CSS_VISIBILITY) === VISIBILITY_VISIBLE) {
        // Fixed-positioned blocks are displayed separately;
        // If we call them now, they will be drawn twice
        if ($child->getCSSProperty(CSS_POSITION) != POSITION_FIXED) {
          if (is_null($child->show_fixed($driver))) {
            return null;
          };
        };
      };
    }

    /** 
     * Restore previous clipping mode, if it have been modified for non-'overflow: visible' 
     * box.
     */
    if ($overflow !== OVERFLOW_VISIBLE) {
      $driver->restore();
    };

    return true;
  }

  function _find(&$box) {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      if ($this->content[$i]->uid == $box->uid) { 
        return $i; 
      };
    }
    return null;
  }

  // Inserts new child box at the specified (zero-based) offset; 0 stands for first child
  // 
  // @param $index index to insert child at
  // @param $box child to be inserted
  //
  function insert_child($index, &$box) {
    $box->parent =& $this;

    // Offset the content array
    for ($i = count($this->content)-1; $i>= $index; $i--) {
      $this->content[$i+1] =& $this->content[$i];
    };

    $this->content[$index] =& $box;
  }

  function insertBefore(&$what, &$where) {
    if ($where) {
      $index = $this->_find($where);

      if (is_null($index)) { 
        return null; 
      };

      $this->insert_child($index, $what);
    } else {
      // If 'where' is not specified, 'what' should become the last child
      $this->add_child($what);
    };
    
    return $what;
  }

  function add_child(&$box) {
    // In general, this function is called like following:
    // $box->add_child(create_pdf_box(...))
    // As create_pdf_box _may_ return null value (for example, for an empty text node),
    // we should process the case of $box == null here
    if ($box) {
      $box->parent =& $this;
      $this->content[] =& $box;
    };
  }

  // Get first child of current box which actually will be drawn 
  // on the page. So, whitespace and null boxes will be ignored
  // 
  // See description of is_null for null box definition.
  // (not only NullBox is treated as null box)
  //
  // @return reference to the first visible child of current box 
  function &get_first() {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      if (!is_whitespace($this->content[$i]) && 
          !$this->content[$i]->is_null()) {
        return $this->content[$i];
      };
    };

    // We use this construct to avoid notice messages in PHP 4.4 and PHP 5
    $dummy = null;
    return $dummy;
  }

  // Get first text or image child of current box which actually will be drawn 
  // on the page. 
  // 
  // See description of is_null for null box definition.
  // (not only NullBox is treated as null box)
  //
  // @return reference to the first visible child of current box 
  function &get_first_data() {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      if (!is_whitespace($this->content[$i]) && !$this->content[$i]->is_null()) {
        if (is_container($this->content[$i])) {
          $data =& $this->content[$i]->get_first_data();
          if (!is_null($data)) { return $data; };
        } else {
          return $this->content[$i];
        };
      };
    };

    // We use this construct to avoid notice messages in PHP 4.4 and PHP 5
    $dummy = null;
    return $dummy;
  }

  // Get last child of current box which actually will be drawn 
  // on the page. So, whitespace and null boxes will be ignored
  // 
  // See description of is_null for null box definition.
  // (not only NullBox is treated as null box)
  //
  // @return reference to the last visible child of current box 
  function &get_last() {
    for ($i=count($this->content)-1; $i>=0; $i--) {
      if (!is_whitespace($this->content[$i]) && !$this->content[$i]->is_null()) {
        return $this->content[$i];
      };
    };

    // We use this construct to avoid notice messages in PHP 4.4 and PHP 5
    $dummy = null;
    return $dummy;
  }

  function offset_if_first(&$box, $dx, $dy) {
    if ($this->is_first($box)) {
      // The top-level box (page box) should never be offset 
      if ($this->parent) {
        if (!$this->parent->offset_if_first($box, $dx, $dy)) {
          $this->offset($dx, $dy);
          return true;
        };
      };
    };
    return false;
  }
    
  function offset($dx, $dy) {
    parent::offset($dx, $dy);

    $this->_current_x += $dx;
    $this->_current_y += $dy;

    // Offset contents
    $size = count($this->content);
    for ($i=0; $i < $size; $i++) {
      $this->content[$i]->offset($dx, $dy);
    }
  }

  function GenericContainerBox() {
    $this->GenericFormattedBox();

    // By default, box does not have any content
    $this->content = array();

    // Initialize line box
    $this->_line = array();

    // Initialize floats-related stuff
    $this->_deferred_floats = array();

    $this->_additional_text_indent = 0;

    // Current-point
    $this->_current_x = 0;
    $this->_current_y = 0;

    // Initialize floating children array
    $this->_floats = array();
  }

  function add_deferred_float(&$float) {
    $this->_deferred_floats[] =& $float;
  }

  /**
   * Create the child nodes of current container object using the parsed HTML data
   *
   * @param mixed $root node corresponding to the current container object
   */
  function create_content(&$root, &$pipeline) {
    // Initialize content
    $child = $root->first_child();
    while ($child) {
      $box_child =& create_pdf_box($child, $pipeline);
      $this->add_child($box_child);
      $child = $child->next_sibling();
    };
  }

  // Content-handling functions

  function is_container() {
    return true;
  }

  // Get total height of this box content (including floats, if any)
  // Note that floats can be contained inside children, so we'll need to use
  // this function recusively 
  function get_real_full_height() {
    $content_size = count($this->content);

    $overflow = $this->getCSSProperty(CSS_OVERFLOW);

    // Treat items with overflow: hidden specifically, 
    // as floats flown out of this boxes will not be visible
    if ($overflow == OVERFLOW_HIDDEN) {
      return $this->get_full_height();
    };

    // Check if this object is totally empty
    $first = $this->get_first();
    if (is_null($first)) { 
      return 0; 
    };

    // Initialize the vertical extent taken by content using the 
    // very first child
    $max_top    = $first->get_top_margin();
    $min_bottom = $first->get_bottom_margin();

    for ($i=0; $i<$content_size; $i++) {     
      if (!$this->content[$i]->is_null()) {
        // Check if top margin of current child is to the up 
        // of vertical extent top margin
        $max_top    = max($max_top, $this->content[$i]->get_top_margin());

        /**
         * Check if current child bottom margin will extend 
         * the vertical space OR if it contains floats extending 
         * this, unless this child have overflow: hidden, because this 
         * will prevent additional content to be visible
         */
        if (!$this->content[$i]->is_container()) {
          $min_bottom = min($min_bottom,
                            $this->content[$i]->get_bottom_margin());
        } else {
          $content_overflow = $this->content[$i]->getCSSProperty(CSS_OVERFLOW);

          if ($content_overflow == OVERFLOW_HIDDEN) {
            $min_bottom = min($min_bottom,
                              $this->content[$i]->get_bottom_margin());
          } else {
            $min_bottom = min($min_bottom,
                              $this->content[$i]->get_bottom_margin(),
                              $this->content[$i]->get_top_margin() - 
                              $this->content[$i]->get_real_full_height());
          };
        };
      };
    }

    return max(0, $max_top - $min_bottom) + $this->_get_vert_extra();
  }

  // LINE-LENGTH RELATED FUNCTIONS

  function _line_length() {
    $sum = 0;
    $size = count($this->_line);

    for ($i=0; $i < $size; $i++) {
      // Note that the line length should include the inline boxes margin/padding
      // as inline boxes are not directly included to the parent line box,
      // we'll need to check the parent of current line box element, 
      // and, if it is an inline box, AND this element is last or first contained element
      // add correcponsing padding value
      $element =& $this->_line[$i];

      if (isset($element->wrapped) && !is_null($element->wrapped)) {
        if ($i==0) {
          $sum += $element->get_full_width() - $element->getWrappedWidth();
        } else {
          $sum += $element->getWrappedWidthAndHyphen();
        };
      } else {
        $sum += $element->get_full_width();
      };

      if ($element->parent) {
        $first = $element->parent->get_first();
        $last  = $element->parent->get_last();

        if (!is_null($first) && $first->uid === $element->uid) { 
          $sum += $element->parent->get_extra_line_left(); 
        }

        if (!is_null($last) && $last->uid === $element->uid) { 
          $sum += $element->parent->get_extra_line_right(); 
        }
      };
    }

    if ($this->_first_line) {
      $ti = $this->getCSSProperty(CSS_TEXT_INDENT);
      $sum += $ti->calculate($this);
      $sum += $this->_additional_text_indent;
    };

    return $sum;
  }

  function _line_length_delta(&$context) {
    return max($this->get_available_width($context) - $this->_line_length(),0);
  }

  /**
   * Get the last box in current line box
   */
  function &last_in_line() {
    $size = count($this->_line);
    if ($size < 1) {
      $dummy = null;
      return $dummy;
    };

    return $this->_line[$size-1];
  }
  
  // WIDTH

  function get_min_width_natural(&$context) {
    $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();
      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);
      return $min_width; 
    }

    /**
     * We need to add text indent size to the width 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_natural($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($context));
      };
    }

    /**
     * Apply width constraint to min width. Return maximal value
     */
    $wc = $this->getCSSProperty(CSS_WIDTH);
    $containing_block =& $this->_get_containing_block();

    $min_width = $minw;
    return $min_width;
  }

  function get_min_width(&$context) {
    $strategy = new StrategyWidthMin();
    return $strategy->apply($this, $context);    
  }

  function get_min_nowrap_width(&$context) {
    $strategy = new StrategyWidthMinNowrap();
    return $strategy->apply($this, $context);
  }

  // Note: <table width="100%" inside some block box cause this box to expand
  // $limit - maximal width which should not be exceeded; by default, there's no limit at all
  // 
  function get_max_width_natural(&$context, $limit=10E6) {
    $strategy = new StrategyWidthMaxNatural($limit);
    return $strategy->apply($this, $context);
  }

  function get_max_width(&$context, $limit=10E6) {
    $strategy = new StrategyWidthMax($limit);
    return $strategy->apply($this, $context);
  }

  function close_line(&$context, $lastline = false) {
    // Align line-box using 'text-align' property
    $size = count($this->_line);

    if ($size > 0) {
      $last_item =& $this->_line[$size-1];
      if (is_whitespace($last_item)) {
        $last_item->width = 0;
        $last_item->height = 0;
      };
    };

    // Note that text-align should not be applied to the block boxes!
    // As block boxes will be alone in the line-box, we can check
    // if the very first box in the line is inline; if not - no justification should be made
    //
    if ($size > 0) {
      if (is_inline($this->_line[0])) {
        $cb = CSSTextAlign::value2pdf($this->getCSSProperty(CSS_TEXT_ALIGN));
        $cb($this, $context, $lastline);
      } else {
        // Nevertheless, CENTER tag and P/DIV with ALIGN attribute set should affect the 
        // position of non-inline children.
        $cb = CSSPseudoAlign::value2pdf($this->getCSSProperty(CSS_HTML2PS_ALIGN));
        $cb($this, $context, $lastline);
      };
    };

    // Apply vertical align to all of the line content
    // first, we need to aling all baseline-aligned boxes to determine the basic line-box height, top and bottom edges
    // then, SUP and SUP positioned boxes (as they can extend the top and bottom edges, but not affected themselves)
    // then, MIDDLE, BOTTOM and TOP positioned boxes in the given order
    //
    $baselined = array();
    $baseline = 0;
    $height = 0;
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);

      if ($vertical_align == VA_BASELINE) {
        // Add current baseline-aligned item to the baseline
        //
        $baselined[] =& $this->_line[$i];

        $baseline = max($baseline, 
                        $this->_line[$i]->default_baseline);
      };
    };

    $size_baselined = count($baselined);
    for ($i=0; $i < $size_baselined; $i++) {
      $baselined[$i]->baseline = $baseline;

      $height = max($height, 
                    $baselined[$i]->get_full_height() + $baselined[$i]->getBaselineOffset(),
                    $baselined[$i]->get_ascender() + $baselined[$i]->get_descender());

    };

    // SUB vertical align
    //
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_SUB) {
        $this->_line[$i]->baseline = 
          $baseline + $this->_line[$i]->get_full_height()/2;
      };
    }

    // SUPER vertical align
    //
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_SUPER) {
        $this->_line[$i]->baseline = $this->_line[$i]->get_full_height()/2;
      };
    }

    // MIDDLE vertical align
    //
    $middle = 0;
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_MIDDLE) {
        $middle = max($middle, $this->_line[$i]->get_full_height() / 2);
      };
    };

    if ($middle * 2 > $height) {
      // Offset already aligned items
      //
      for ($i=0; $i < $size; $i++) {
        $this->_line[$i]->baseline += ($middle - $height/2);
      };      
      $height = $middle * 2;
    };
 
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_MIDDLE) {
        $this->_line[$i]->baseline = $this->_line[$i]->default_baseline + ($height/2 - $this->_line[$i]->get_full_height()/2);
      };
    }

    // BOTTOM vertical align
    //
    $bottom = 0;
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_BOTTOM) {
        $bottom = max($bottom, $this->_line[$i]->get_full_height());
      };
    };

    if ($bottom > $height) {
      // Offset already aligned items
      //
      for ($i=0; $i < $size; $i++) {
        $this->_line[$i]->baseline += ($bottom - $height);
      };      
      $height = $bottom;
    };

    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_BOTTOM) {
        $this->_line[$i]->baseline = $this->_line[$i]->default_baseline + $height - $this->_line[$i]->get_full_height();
      };
    }

    // TOP vertical align
    //
    $bottom = 0;
    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_TOP) {
        $bottom = max($bottom, $this->_line[$i]->get_full_height());
      };
    };

    if ($bottom > $height) {
      $height = $bottom;
    };

    for ($i=0; $i < $size; $i++) {
      $vertical_align = $this->_line[$i]->getCSSProperty(CSS_VERTICAL_ALIGN);
      if ($vertical_align == VA_TOP) {
        $this->_line[$i]->baseline = $this->_line[$i]->default_baseline;
      };
    }

    // Calculate the bottom Y coordinate of last line box
    //
    $line_bottom = $this->_current_y;
    foreach ($this->_line AS $line_element) {
      // This line is required; say, we have sequence of text and image inside the container,
      // AND image have greater baseline than text; in out case, text will be offset to the bottom 
      // of the page and we lose the gap between text and container bottom edge, unless we'll re-extend
      // containier height

      // Note that we're using the colapsed margin value to get the Y coordinate to extend height to,
      // as bottom margin may be collapsed with parent

      $effective_bottom = 
        $line_element->get_top() - 
        $line_element->get_height();

      $this->extend_height($effective_bottom);
      $line_bottom = min($effective_bottom, $line_bottom);
    }

    $this->extend_height($line_bottom);

    // Clear the line box
    $this->_line = array();

    // Reset current X coordinate to the far left
    $this->_current_x = $this->get_left();
    
    // Extend Y coordinate
    $this->_current_y = $line_bottom;

    // Render the deferred floats
    for ($i = 0, $size = count($this->_deferred_floats); $i < $size; $i++) {
      $this->_deferred_floats[$i]->reflow_static_float($this, $context);
    };
    // Clear deferred float list
    $this->_deferred_floats = array();

    // modify the current-x value, so that next inline box will not intersect any floating boxes
    $this->_current_x = $context->float_left_x($this->_current_x, $this->_current_y);

    $this->_first_line = false;
  }

  function append_line(&$item) {
    $this->_line[] =& $item;
  }

  // Line box should be treated as empty in following cases: 
  // 1. It is really empty (so, it contains 0 boxes)
  // 2. It contains only whitespace boxes
  function line_box_empty() {
    $size = count($this->_line);
    if ($size == 0) { return true; }

    // Scan line box
    for ($i=0; $i<$size; $i++) {
      if (!is_whitespace($this->_line[$i]) && 
          !$this->_line[$i]->is_null()) { return false; };
    }

    // No non-whitespace boxes were found
    return true;
  }

  function reflow_anchors(&$viewport, &$anchors) {
    GenericFormattedBox::reflow_anchors($viewport, $anchors);

    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      $this->content[$i]->reflow_anchors($viewport, $anchors);
    }
  }

  function fitFloats(&$context) {
    $float_bottom = $context->float_bottom();     
    if (!is_null($float_bottom)) { 
      $this->extend_height($float_bottom); 
    };
    
    $float_right = $context->float_right();
    if (!is_null($float_right)) { 
      $this->extend_width($float_right); 
    };
  }

  function reflow_content(&$context) {
    $text_indent = $this->getCSSProperty(CSS_TEXT_INDENT);

    $this->close_line($context);

    $this->_first_line = true;

    // If first child is inline - apply text-indent
    $first = $this->get_first();
    if (!is_null($first)) {
      if (is_inline($first)) {
        $this->_current_x += $text_indent->calculate($this);
        $this->_current_x += $this->_additional_text_indent;
      };
    };

    $this->height = 0;
    // Reset current Y value
    $this->_current_y = $this->get_top();

    $size = count($this->content);
    for ($i=0; $i < $size; $i++) {
      $child =& $this->content[$i];
      $child->reflow($this, $context);
    };

    $this->close_line($context, true);
  }

  function reflow_inline() {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      $this->content[$i]->reflow_inline();
    };
  }

  function reflow_text(&$viewport) {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      if (is_null($this->content[$i]->reflow_text($viewport))) {
        return null;
      };
    }
    return true;
  }

  /**
   * Position/size current box as floating one
   */
  function reflow_static_float(&$parent, &$context) {
    // Defer the float rendering till the next line box
    if (!$parent->line_box_empty()) {
      $parent->add_deferred_float($this);
      return;
    };

    // Calculate margin values if they have been set as a percentage
    $this->_calc_percentage_margins($parent);
    $this->_calc_percentage_padding($parent);

    // Calculate width value if it have been set as a percentage
    $this->_calc_percentage_width($parent, $context);

    // Calculate margins and/or width is 'auto' values have been specified
    $this->_calc_auto_width_margins($parent);

    // Determine the actual width of the floating box
    // Note that get_max_width returns both content and extra width
    $this->put_full_width($this->get_max_width_natural($context, $this->parent->get_width()));

    // We need to call this function before determining the horizontal coordinate
    // as after vertical offset the additional space to the left may apperar
    $y = $this->apply_clear($parent->_current_y, $context);

    // determine the position of top-left floating box corner
    if ($this->getCSSProperty(CSS_FLOAT) === FLOAT_RIGHT) {
      $context->float_right_xy($parent, $this->get_full_width(), $x, $y);
      $x -= $this->get_full_width();
    } else {
      $context->float_left_xy($parent, $this->get_full_width(), $x, $y);
    };

    // Note that $x and $y contain just a free space corner coordinate;
    // If our float has a margin/padding space, we'll need to offset ot a little;
    // Remember that float margins are never collapsed!
    $this->moveto($x + $this->get_extra_left(), $y - $this->get_extra_top());  

    // Reflow contents. 
    // Note that floating box creates a new float flow context for it children.

    $context->push_floats();

    // Floating box create a separate margin collapsing context
    $context->push_collapsed_margin(0);

    $this->reflow_content($context); 

    $context->pop_collapsed_margin();

    // Floats and boxes with overflow: hidden
    // should completely enclose its child floats
    $this->fitFloats($context);

    // restore old float flow context
    $context->pop_floats();
    
    // Add this  box to the list of floats in current context
    $context->add_float($this);

    // Now fix the value of _current_x for the parent box; it is required
    // in the following case:
    // <body><img align="left">some text
    // in such situation floating image is flown immediately, but it the close_line call have been made before, 
    // so _current_x value of container box will be still equal to ots left content edge; by calling float_left_x again,
    // we'll force "some text" to be offset to the right
    $parent->_current_x = $context->float_left_x($parent->_current_x, $parent->_current_y);
  }  

  function reflow_whitespace(&$linebox_started, &$previous_whitespace) {
    $previous_whitespace = false;
    $linebox_started = false;

    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      $child =& $this->content[$i];

      $child->reflow_whitespace($linebox_started, $previous_whitespace);      
    };

    // remove the last whitespace in block box
    $this->remove_last_whitespace();

    // Non-inline box have terminated; we may be sure that line box will be closed
    // at this moment and new line box after this will be generated
    if (!is_inline($this)) { 
      $linebox_started = false; 
    };

    return;
  }

  function remove_last_whitespace() {
    if (count($this->content) == 0) { 
      return; 
    };

    $i = count($this->content)-1;
    $last = $this->content[$i];
    while ($i >= 0 && is_whitespace($this->content[$i])) {
      $this->remove($this->content[$i]);
      
      $i --;
      if ($i >= 0) {
        $last = $this->content[$i];
      };
    };

    if ($i >= 0) {
      if (is_container($this->content[$i])) {
        $this->content[$i]->remove_last_whitespace();
      };
    };
  }

  function remove(&$box) {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      if ($this->content[$i]->uid === $box->uid) {
        $this->content[$i] = NullBox::create();
      };
    };

    return;
  }

  function is_first(&$box) {
    $first =& $this->get_first();

    // Check if there's no first box at all
    //
    if (is_null($first)) { return false; };

    return $first->uid == $box->uid;
  }

  function is_null() {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      if (!$this->content[$i]->is_null()) { return false; };
    };
    return true;
  }

  // Calculate the available widths - e.g. content width minus space occupied by floats;
  // as floats may not fill the whole height of this box, this value depends on Y-coordinate.
  // We use current_Y in calculations
  //
  function get_available_width(&$context) {
    $left_float_width = $context->float_left_x($this->get_left(), $this->_current_y) - $this->get_left();
    $right_float_width = $this->get_right() - $context->float_right_x($this->get_right(), $this->_current_y);
    return $this->get_width() - $left_float_width - $right_float_width;
  }

  function pre_reflow_images() {
    $size = count($this->content);
    for ($i=0; $i<$size; $i++) {
      $this->content[$i]->pre_reflow_images();
    };
  }

  function _setupClip(&$driver) {
    if (!is_null($this->parent)) {
      $this->parent->_setupClip($driver);
    };

    $overflow = $this->getCSSProperty(CSS_OVERFLOW);
    if ($overflow !== OVERFLOW_VISIBLE) {
      $driver->moveto( $this->get_left_border() , $this->get_top_border());
      $driver->lineto( $this->get_right_border(), $this->get_top_border());
      $driver->lineto( $this->get_right_border(), $this->get_bottom_border());
      $driver->lineto( $this->get_left_border() , $this->get_bottom_border());
      $driver->closepath();
      $driver->clip();
    };
  }

  /**
   * DOMish functions
   */
  function &get_element_by_id($id) {
    if (isset($GLOBALS['__html_box_id_map'])) {
      return $GLOBALS['__html_box_id_map'][$id];
    } else {
      $dummy = null;
      return $dummy;
    };
  }
  
  /* 
   *  this is just a fake at the moment
   */
  function get_body() {
    return $this;
  }

  function getChildNodes() {
    return $this->content;
  }
}

?>