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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
<?php
// $Header: /cvsroot/html2ps/box.generic.formatted.php,v 1.21 2007/02/18 09:55:10 Konstantin Exp $

require_once(HTML2PS_DIR.'doc.anchor.class.php');
require_once(HTML2PS_DIR.'layout.vertical.php');

class GenericFormattedBox extends GenericBox {
  var $uid;

  function _get_collapsable_top_margin_internal() {
    $positive_margin = 0;
    $negative_margin = 0;

    $current_box = $this;

    $border  = $current_box->getCSSProperty(CSS_BORDER);
    $padding = $current_box->getCSSProperty(CSS_PADDING);
    if ($border->top->get_width() > 0 ||
        $padding->top->value > 0) {
      return 0;
    };

    while (!is_null($current_box) && 
           $current_box->isBlockLevel()) {
      $margin  = $current_box->getCSSProperty(CSS_MARGIN);
      $border  = $current_box->getCSSProperty(CSS_BORDER);
      $padding = $current_box->getCSSProperty(CSS_PADDING);

      $top_margin = $margin->top->value;

      if ($top_margin >= 0) {
        $positive_margin = max($positive_margin, $top_margin);
      } else {
        $negative_margin = min($negative_margin, $top_margin);
      };

      if ($border->top->get_width() > 0 ||
          $padding->top->value > 0) {
        $current_box = null;
      } else {
        $current_box = $current_box->get_first();
      };
    };

    return $positive_margin /*- $negative_margin*/;
  }

  function _get_collapsable_top_margin_external() {
    $positive_margin = 0;
    $negative_margin = 0;

    $current_box = $this;
    while (!is_null($current_box) && 
           $current_box->isBlockLevel()) {
      $margin  = $current_box->getCSSProperty(CSS_MARGIN);
      $border  = $current_box->getCSSProperty(CSS_BORDER);
      $padding = $current_box->getCSSProperty(CSS_PADDING);

      $top_margin = $margin->top->value;

      if ($top_margin >= 0) {
        $positive_margin = max($positive_margin, $top_margin);
      } else {
        $negative_margin = min($negative_margin, $top_margin);
      };

      if ($border->top->get_width() > 0 ||
          $padding->top->value > 0) {
        $current_box = null;
      } else {
        $current_box = $current_box->get_first();
      };
    };

    return $positive_margin + $negative_margin;
  }

  function _get_collapsable_bottom_margin_external() {
    $positive_margin = 0;
    $negative_margin = 0;

    $current_box = $this;
    while (!is_null($current_box) && 
           $current_box->isBlockLevel()) {
      $margin  = $current_box->getCSSProperty(CSS_MARGIN);
      $border  = $current_box->getCSSProperty(CSS_BORDER);
      $padding = $current_box->getCSSProperty(CSS_PADDING);

      $bottom_margin = $margin->bottom->value;

      if ($bottom_margin >= 0) {
        $positive_margin = max($positive_margin, $bottom_margin);
      } else {
        $negative_margin = min($negative_margin, $bottom_margin);
      };

      if ($border->bottom->get_width() > 0 ||
          $padding->bottom->value > 0) {
        $current_box = null;
      } else {
        $current_box = $current_box->get_last();
      };
    };

    return $positive_margin + $negative_margin;
  }

  function collapse_margin_bottom(&$parent, &$context) {
    /** 
     * Now, if there's a parent for this box, we extend its height to fit current box.
     * If parent generated new flow context (like table cell or floating box), its content 
     * area should include the current box bottom margin (bottom margin does not colllapse). 
     * See CSS 2.1 for more detailed explanations.
     *
     * @see FlowContext::container_uid()
     *
     * @link http://www.w3.org/TR/CSS21/visudet.html#Computing_widths_and_margins CSS 2.1 8.3.1 Calculating widths and margins
     */
    $parent_border  = $parent->getCSSProperty(CSS_BORDER);
    $parent_padding = $parent->getCSSProperty(CSS_PADDING);

    /**
     * The  bottom margin  of an  in-flow block-level  element  with a
     * 'height'  of 'auto'  and 'min-height'  less than  the element's
     * used height  and 'max-height'  greater than the  element's used
     * height  is adjoining  to its  last in-flow  block-level child's
     * bottom margin if the element has NO BOTTOM PADDING OR BORDER.
     */
    
    $last =& $parent->get_last();
    if (!is_null($last) && 
        $last->uid == $this->uid &&                  // This element is a last in-flow block level element AND
        $parent->uid != $context->container_uid() && // Parent element did not generate new flow context (like table-cell) AND
        $parent_border->bottom->get_width() == 0  && // Parent have NO bottom border AND
        $parent_padding->bottom->value == 0)  {      // Parent have NO bottom padding AND
      $parent->extend_height($this->get_bottom_border());
    } else {
      // Otherwise (in particular, if this box is not last), bottom
      // margin of the current box will be contained inside the current box
      $parent->extend_height($this->get_bottom_margin());      
    }

    $cm = $context->get_collapsed_margin();
    $context->pop_collapsed_margin();
    $context->pop_collapsed_margin();

    /**
     * shift current parent 'watermark' to the current box margin edge; 
     * all content now will be drawn below this mark (with a small exception 
     * of elements having negative vertical margins, of course).
     */
    if ($parent_border->bottom->get_width() > 0 ||
        $parent_padding->bottom->value > 0) {
      $context->push_collapsed_margin( 0 );
      return $this->get_bottom_border() - $cm;
    } else {
      $collapsable = $this->_get_collapsable_bottom_margin_external();
      $context->push_collapsed_margin( $collapsable );

      return $this->get_bottom_border();
    };
  }

  function collapse_margin(&$parent, &$context) {
    // Do margin collapsing

    // Margin collapsing is done as follows:
    // 1. If previous sibling was an inline element (so, parent line box was not empty),
    //    then no collapsing will take part
    // 2. If NO previous element exists at all, then collapse current box top margin
    //    with parent's collapsed top margin.
    // 2.1. If parent element was float, no collapsing should be
    // 3. If there's previous block element, collapse current box top margin 
    //    with previous elemenent's collapsed bottom margin
      
    // Check if current parent line box contains inline elements only. In this case the only 
    // margin will be current box margin 

    if (!$parent->line_box_empty()) {
      // Case (1). Previous element was inline element; no collapsing

      $parent->close_line($context);

      $vmargin = $this->_get_collapsable_top_margin_external();
    } else {   
      $parent_first = $this->parent->get_first();

      if (is_null($parent_first) || // Unfortunately, we sometimes get null as a value of $parent_first; this should be checked
          $parent_first->uid == $this->uid) {
        // Case (2). No previous block element at all; Collapse with parent margins
        $collapsable = $this->_get_collapsable_top_margin_external();
        $collapsed   = $context->get_collapsed_margin();

        $vmargin = max(0, $collapsable - $collapsed);

      } else {
        // Case (3). There's a previous block element

        $collapsable = $this->_get_collapsable_top_margin_external();
        $collapsed   = $context->get_collapsed_margin();
        
        // In this case, base position is a bottom border of the previous element
        // $vmargin - offset from a base position - should be at least $collapsed
        // (value of collapsed bottom margins from the previous element and its
        // children). If current element have $collapsable - collapsed top margin 
        // (from itself and children too) greater that this value, we should 
        // offset it further to the bottom

        $vmargin = max($collapsable, $collapsed);
      };
    };

    // Determine the base Y coordinate of box margin edge
    $y = $parent->_current_y - $vmargin;

    $internal_margin = $this->_get_collapsable_top_margin_internal();
    $context->push_collapsed_margin($internal_margin);

    return $y;
  }

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

    // Layout data
    $this->baseline = 0;
    $this->parent = null;
  }

  function readCSS(&$state) {
    parent::readCSS($state);
    
    $this->_readCSS($state,
                    array(CSS_OVERFLOW,
                          CSS_PAGE_BREAK_AFTER,
                          CSS_PAGE_BREAK_BEFORE,
                          CSS_PAGE_BREAK_INSIDE,
                          CSS_ORPHANS,
                          CSS_WIDOWS,
                          CSS_POSITION,
                          CSS_TEXT_ALIGN,
                          CSS_WHITE_SPACE,
                          CSS_CLEAR,
                          CSS_CONTENT,
                          CSS_HTML2PS_PSEUDOELEMENTS,
                          CSS_FLOAT,
                          CSS_Z_INDEX,
                          CSS_HTML2PS_ALIGN,
                          CSS_HTML2PS_NOWRAP,
                          CSS_DIRECTION,
                          CSS_PAGE));
 
    $this->_readCSSLengths($state,
                           array(CSS_BACKGROUND,
                                 CSS_BORDER,
                                 CSS_BOTTOM,
                                 CSS_TOP,
                                 CSS_LEFT, 
                                 CSS_RIGHT,
                                 CSS_MARGIN,
                                 CSS_PADDING,
                                 CSS_TEXT_INDENT,
                                 CSS_HTML2PS_COMPOSITE_WIDTH,
                                 CSS_HEIGHT,
                                 CSS_MIN_HEIGHT,
                                 CSS_MAX_HEIGHT,
                                 CSS_LETTER_SPACING
                                 ));   

    /**
     * CSS 2.1,  p 8.5.2: 
     *
     * If an  element's border  color is not  specified with  a border
     * property,  user agents  must  use the  value  of the  element's
     * 'color' property as the computed value for the border color.
     */
    $border =& $this->getCSSProperty(CSS_BORDER);
    $color  =& $this->getCSSProperty(CSS_COLOR);

    if ($border->top->isDefaultColor()) {
      $border->top->setColor($color);
    };

    if ($border->right->isDefaultColor()) {
      $border->right->setColor($color);
    };

    if ($border->bottom->isDefaultColor()) {
      $border->bottom->setColor($color);
    };

    if ($border->left->isDefaultColor()) {
      $border->left->setColor($color);
    };

    $this->setCSSProperty(CSS_BORDER, $border);

    $this->_height_constraint =& HCConstraint::create($this);
    $this->height = 0;

    // 'width'
    $wc =& $this->getCSSProperty(CSS_WIDTH);
    $this->width = $wc->apply(0,0);

    // 'PSEUDO-CSS' properties

    // '-localalign'
    switch ($state->getProperty(CSS_HTML2PS_LOCALALIGN)) {
    case LA_LEFT:
      break;
    case LA_RIGHT:
      $margin =& $this->getCSSProperty(CSS_MARGIN);
      $margin->left->auto = true;
      $this->setCSSProperty(CSS_MARGIN, $margin);
      break;
    case LA_CENTER:
      $margin =& $this->getCSSProperty(CSS_MARGIN);
      $margin->left->auto  = true;
      $margin->right->auto = true;
      $this->setCSSProperty(CSS_MARGIN, $margin);
      break;
    };
  }

  function _calc_percentage_margins(&$parent) {
    $margin = $this->getCSSProperty(CSS_MARGIN);
    $containing_block =& $this->_get_containing_block();
    $margin->calcPercentages($containing_block['right'] - $containing_block['left']);
    $this->setCSSProperty(CSS_MARGIN, $margin);
  }

  function _calc_percentage_padding(&$parent) {
    $padding = $this->getCSSProperty(CSS_PADDING);
    $containing_block =& $this->_get_containing_block();
    $padding->calcPercentages($containing_block['right'] - $containing_block['left']);
    $this->setCSSProperty(CSS_PADDING, $padding);
  }

  function apply_clear($y, &$context) {
    return LayoutVertical::apply_clear($this, $y, $context);
  }


  /**
   * CSS 2.1:
   * 10.2 Content width: the 'width' property
   * Values have the following meanings:
   * <percentage> Specifies a percentage width. The percentage is calculated with respect to the width of the generated box's containing block.
   *
   * If the containing block's width depends on this element's width, 
   * then the resulting layout is undefined in CSS 2.1. 
   */
  function _calc_percentage_width(&$parent, &$context) {
    $wc = $this->getCSSProperty(CSS_WIDTH);
    if ($wc->isFraction()) { 
      $containing_block =& $this->_get_containing_block();

      // Calculate actual width
      $width = $wc->apply($this->width, $containing_block['right'] - $containing_block['left']);

      // Assign calculated width
      $this->put_width($width);
        
      // Remove any width constraint
      $this->setCSSProperty(CSS_WIDTH, new WCConstant($width));
    }
  }

  function _calc_auto_width_margins(&$parent) {
    $float = $this->getCSSProperty(CSS_FLOAT);

    if ($float !== FLOAT_NONE) {
      $this->_calc_auto_width_margins_float($parent);
    } else {
      $this->_calc_auto_width_margins_normal($parent);
    }
  }

  // 'auto' margin value became 0, 'auto' width is 'shrink-to-fit'
  function _calc_auto_width_margins_float(&$parent) {
    // If 'width' is set to 'auto' the used value is the "shrink-to-fit" width
    // TODO
    if (false) {
      // Calculation of the shrink-to-fit width is similar to calculating the
      // width of a table cell using the automatic table layout
      // algorithm. Roughly: calculate the preferred width by formatting the
      // content without breaking lines other than where explicit line breaks
      // occur, and also calculate the preferred minimum width, e.g., by trying
      // all possible line breaks. CSS 2.1 does not define the exact
      //  algorithm. Thirdly, find the available width: in this case, this is
      // the width of the containing block minus minus the used values of
      // 'margin-left', 'border-left-width', 'padding-left', 'padding-right',
      //  'border-right-width', 'margin-right', and the widths of any relevant
      // scroll bars.
      
      // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
      
      // Store used value
    };
  
    // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
    $margin = $this->getCSSProperty(CSS_MARGIN);
    if ($margin->left->auto) { $margin->left->value = 0; }
    if ($margin->right->auto) { $margin->right->value = 0; }
    $this->setCSSProperty(CSS_MARGIN, $margin);

    $this->width = $this->get_width();
  }

  // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
  function _calc_auto_width_margins_normal(&$parent) {
    // get the containing block width
    $containing_block =& $this->_get_containing_block();
    $parent_width = $containing_block['right'] - $containing_block['left'];
   
    // If 'width' is set to 'auto', any other 'auto' values become '0'  and 'width' follows from the resulting equality.

    // If both 'margin-left' and 'margin-right' are 'auto', their used values are equal. 
    // This horizontally centers the element with respect to the edges of the containing block.
    
    $margin = $this->getCSSProperty(CSS_MARGIN);
    if ($margin->left->auto && $margin->right->auto) {
      $margin_value = ($parent_width - $this->get_full_width()) / 2;
      $margin->left->value = $margin_value;
      $margin->right->value = $margin_value;
    } else {
      // If there is exactly one value specified as 'auto', its used value follows from the equality.
      if ($margin->left->auto) {
        $margin->left->value = $parent_width - $this->get_full_width();
      } elseif ($margin->right->auto) {
        $margin->right->value = $parent_width - $this->get_full_width();
      };
    };
    $this->setCSSProperty(CSS_MARGIN, $margin);
    
    $this->width = $this->get_width();
  }

  function get_descender() {
    return 0;
  }

  function get_ascender() {
    return 0;
  }

  function _get_vert_extra() {
    return
      $this->get_extra_top() +
      $this->get_extra_bottom();
  }

  function _get_hor_extra() {
    return
      $this->get_extra_left() + 
      $this->get_extra_right();
  }
  
  // Width:
  // 'get-min-width' stub
  function get_min_width(&$context) {
    die("OOPS! Unoverridden get_min_width called in class ".get_class($this)." inside ".get_class($this->parent));
  }

  function get_preferred_width(&$context) {
    return $this->get_max_width($context);
  }

  function get_preferred_minimum_width(&$context) {
    return $this->get_min_width($context);
  }

  // 'get-max-width' stub
  function get_max_width(&$context) {
    die("OOPS! Unoverridden get_max_width called in class ".get_class($this)." inside ".get_class($this->parent));
  }

  function get_max_width_natural(&$context) {
    return $this->get_max_width($context);
  }

  function get_full_width() { 
    return $this->get_width() + $this->_get_hor_extra(); 
  }

  function put_full_width($value) {
    // Calculate value of additional horizontal space consumed by margins and padding
    $this->width = $value - $this->_get_hor_extra();
  }

  function &_get_containing_block() {
    $position = $this->getCSSProperty(CSS_POSITION);

    switch ($position) {
    case POSITION_ABSOLUTE:
      $containing_block =& $this->_get_containing_block_absolute();
      return $containing_block;
    case POSITION_FIXED:
      $containing_block =& $this->_get_containing_block_fixed();
      return $containing_block;
    case POSITION_STATIC:
    case POSITION_RELATIVE:
      $containing_block =& $this->_get_containing_block_static();
      return $containing_block;
    default:
      die(sprintf('Unexpected position enum value: %d', $position));
    };
  }

  function &_get_containing_block_fixed() {
    $media = $GLOBALS['g_media'];

    $containing_block = array();
    $containing_block['left']   = mm2pt($media->margins['left']);
    $containing_block['right']  = mm2pt($media->margins['left'] + $media->real_width());
    $containing_block['top']    = mm2pt($media->margins['bottom'] + $media->real_height());
    $containing_block['bottom'] = mm2pt($media->margins['bottom']);

    return $containing_block;    
  }

  // Get the position and size of containing block for current 
  // ABSOLUTE POSITIONED element. It is assumed that this function
  // is called for ABSOLUTE positioned boxes ONLY
  //
  // @return associative array with 'top', 'bottom', 'right' and 'left' 
  // indices in data space describing the position of containing block
  //
  function &_get_containing_block_absolute() {
    $parent =& $this->parent;

    // No containing block at all... 
    // How could we get here?
    if (is_null($parent)) { 
      trigger_error("No containing block found for absolute-positioned element",
                    E_USER_ERROR);
    };

    // CSS 2.1:
    // If the element has 'position: absolute', the containing block is established by the 
    // nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:
    // - In the case that the ancestor is inline-level, the containing block depends on 
    //   the 'direction' property of the ancestor:
    //   1. If the 'direction' is 'ltr', the top and left of the containing block are the top and left 
    //      content edges of the first box generated by the ancestor, and the bottom and right are the 
    //      bottom and right content edges of the last box of the ancestor.
    //   2. If the 'direction' is 'rtl', the top and right are the top and right edges of the first
    //      box generated by the ancestor, and the bottom and left are the bottom and left content 
    //      edges of the last box of the ancestor.
    // - Otherwise, the containing block is formed by the padding edge of the ancestor.
    // TODO: inline-level ancestors
    while ((!is_null($parent->parent)) && 
           ($parent->getCSSProperty(CSS_POSITION) === POSITION_STATIC)) { 
      $parent =& $parent->parent; 
    }

    // Note that initial containg block (containig BODY element) will be formed by BODY margin edge,
    // unlike other blocks which are formed by padding edges
    
    if ($parent->parent) {
      // Normal containing block
      $containing_block = array();
      $containing_block['left']   = $parent->get_left_padding();
      $containing_block['right']  = $parent->get_right_padding();
      $containing_block['top']    = $parent->get_top_padding();
      $containing_block['bottom'] = $parent->get_bottom_padding();
    } else {
      // Initial containing block 
      $containing_block = array();
      $containing_block['left']   = $parent->get_left_margin();
      $containing_block['right']  = $parent->get_right_margin();
      $containing_block['top']    = $parent->get_top_margin();
      $containing_block['bottom'] = $parent->get_bottom_margin();
    };

    return $containing_block;
  }

  function &_get_containing_block_static() {
    $parent =& $this->parent;
    
    // No containing block at all... 
    // How could we get here?

    if (is_null($parent)) { 
      die("No containing block found for static-positioned element"); 
    };
    
    while (!is_null($parent->parent) && 
           !$parent->isBlockLevel() && 
           !$parent->isCell()) { 
      $parent =& $parent->parent; 
    };

    // Note that initial containg block (containing BODY element) 
    // will be formed by BODY margin edge,
    // unlike other blocks which are formed by content edges
    
    $containing_block = array();
    $containing_block['left']   = $parent->get_left();
    $containing_block['right']  = $parent->get_right();
    $containing_block['top']    = $parent->get_top();
    $containing_block['bottom'] = $parent->get_bottom();

    return $containing_block;
  }

  // Height constraint 
  function get_height_constraint() {
    return $this->_height_constraint;
  }
  
  function put_height_constraint(&$wc) {
    $this->_height_constraint = $wc;
  }

  // Extends the box height to cover the given Y coordinate
  // If box height is already big enough, no changes will be made
  //
  // @param $y_coord Y coordinate should be covered by the box
  //
  function extend_height($y_coord) {
    $this->put_height(max($this->get_height(), $this->get_top() - $y_coord));
  }

  function extend_width($x_coord) {
    $this->put_width(max($this->get_width(), $x_coord - $this->get_left()));
  }

  function get_extra_bottom() {
    $border = $this->getCSSProperty(CSS_BORDER);
    return 
      $this->get_margin_bottom() + 
      $border->bottom->get_width() + 
      $this->get_padding_bottom();
  }

  function get_extra_left() {
    $border = $this->getCSSProperty(CSS_BORDER);

    $left_border = $border->left;

    return 
      $this->get_margin_left() + 
      $left_border->get_width() + 
      $this->get_padding_left();
  }

  function get_extra_right() {
    $border = $this->getCSSProperty(CSS_BORDER);
    $right_border = $border->right;
    return 
      $this->get_margin_right() + 
      $right_border->get_width() + 
      $this->get_padding_right();
  }

  function get_extra_top() {
    $border = $this->getCSSProperty(CSS_BORDER);
    return 
      $this->get_margin_top() + 
      $border->top->get_width() + 
      $this->get_padding_top();
  }

  function get_extra_line_left() { return 0; }
  function get_extra_line_right() { return 0; }

  function get_margin_bottom() { 
    $margin = $this->getCSSProperty(CSS_MARGIN);
    return $margin->bottom->value; 
  }

  function get_margin_left() { 
    $margin = $this->getCSSProperty(CSS_MARGIN);
    return $margin->left->value; 
  }
  
  function get_margin_right() { 
    $margin = $this->getCSSProperty(CSS_MARGIN);
    return $margin->right->value; 
  }

  function get_margin_top() { 
    $margin = $this->getCSSProperty(CSS_MARGIN);
    return $margin->top->value; 
  }

  function get_padding_right() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $padding->right->value; 
  }

  function get_padding_left() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $padding->left->value; 
  }

  function get_padding_top() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $padding->top->value; 
  }

  function get_border_top_width() { 
    return $this->border->top->width; 
  }

  function get_padding_bottom() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $padding->bottom->value; 
  }

  function get_left_border() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    $border  = $this->getCSSProperty(CSS_BORDER);

    return 
      $this->get_left() - 
      $padding->left->value - 
      $border->left->get_width(); 
  }

  function get_right_border() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    $border  = $this->getCSSProperty(CSS_BORDER);

    return 
      $this->get_left() + 
      $this->get_width() + 
      $padding->right->value + 
      $border->right->get_width(); 
  }

  function get_top_border()     { 
    $border = $this->getCSSProperty(CSS_BORDER);

    return 
      $this->get_top_padding() + 
      $border->top->get_width();  
  }

  function get_bottom_border()  { 
    $border = $this->getCSSProperty(CSS_BORDER);
    return 
      $this->get_bottom_padding()  - 
      $border->bottom->get_width();  
  }

  function get_left_padding() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $this->get_left() - $padding->left->value; 
  }

  function get_right_padding() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $this->get_left() + $this->get_width() + $padding->right->value; 
  }

  function get_top_padding()    { 
    $padding = $this->getCSSProperty(CSS_PADDING);

    return 
      $this->get_top() + 
      $padding->top->value; 
  }

  function get_bottom_padding() { 
    $padding = $this->getCSSProperty(CSS_PADDING);
    return $this->get_bottom() - $padding->bottom->value;
  }

  function get_left_margin()    { 
    return 
      $this->get_left() - 
      $this->get_extra_left();
  }

  function get_right_margin()   { 
    return 
      $this->get_right() + 
      $this->get_extra_right();
  }

  function get_bottom_margin()  { 
    return 
      $this->get_bottom() - 
      $this->get_extra_bottom();
  }

  function get_top_margin() { 
    $margin = $this->getCSSProperty(CSS_MARGIN);

    return 
      $this->get_top_border() + 
      $margin->top->value; 
  }

  // Geometry
  function contains_point_margin($x, $y) {
    // Actually, we treat a small area around the float as "inside" float;
    // it will help us to prevent incorrectly positioning float due the rounding errors
    $eps = 0.1;
    return 
      $this->get_left_margin()-$eps   <= $x &&
      $this->get_right_margin()+$eps  >= $x &&
      $this->get_top_margin()+$eps    >= $y &&
      $this->get_bottom_margin()      <  $y;
  }

  function get_width() {
    $wc = $this->getCSSProperty(CSS_WIDTH);

    if ($this->parent) {
      return $wc->apply($this->width, $this->parent->width);
    } else {
      return $wc->apply($this->width, $this->width);
    }
  }
  
  // Unlike real/constrained width, or min/max width,
  // expandable width shows the size current box CAN be expanded;
  // it is pretty obvious that width-constrained boxes will never be expanded;
  // any other box can be expanded up to its parent _expandable_ width - 
  // as parent can be expanded too. 
  //
  function get_expandable_width() {
    $wc = $this->getCSSProperty(CSS_WIDTH);
    if ($wc->isNull() && $this->parent) {
      return $this->parent->get_expandable_width();
    } else {
      return $this->get_width();
    };
  }

  function put_width($value) {
    // TODO: constraints
    $this->width = $value;
  }

  function get_height() {
    if ($this->_height_constraint->applicable($this)) {
      return $this->_height_constraint->apply($this->height, $this);
    } else {
      return $this->height;
    };
  }

  function get_height_padded() {
    return $this->get_height() + $this->get_padding_top() + $this->get_padding_bottom();
  }

  function put_height($value) {
    if ($this->_height_constraint->applicable($this)) {
      $this->height = $this->_height_constraint->apply($value, $this);
    } else {
      $this->height = $value;
    };
  }

  function put_full_height($value) {
    $this->put_height($value - $this->_get_vert_extra());
  }

  // Returns total height of current element: 
  // top padding + top margin + content + bottom padding + bottom margin + top border + bottom border
  function get_full_height() {
    return $this->get_height() + 
      $this->get_extra_top() +
      $this->get_extra_bottom();
  }

  function get_real_full_height() { 
    return $this->get_full_height(); 
  }

  function out_of_flow() {
    $position = $this->getCSSProperty(CSS_POSITION);
    $display  = $this->getCSSProperty(CSS_DISPLAY);

    return 
      $position == POSITION_ABSOLUTE ||
      $position == POSITION_FIXED ||
      $display == 'none';
  }

  function moveto($x, $y) { $this->offset($x - $this->get_left(), $y - $this->get_top()); }

  function show(&$viewport) {
    $border     = $this->getCSSProperty(CSS_BORDER);
    $background = $this->getCSSProperty(CSS_BACKGROUND);

    // Draw border of the box
    $border->show($viewport, $this);

    // Render background of the box
    $background->show($viewport, $this);

    parent::show($viewport);

    return true;
  }

  function show_fixed(&$viewport) {
    return $this->show($viewport);
  }

  function is_null() { 
    return false; 
  }

  function line_break_allowed() { 
    $white_space = $this->getCSSProperty(CSS_WHITE_SPACE);
    $nowrap      = $this->getCSSProperty(CSS_HTML2PS_NOWRAP);

    return 
      ($white_space === WHITESPACE_NORMAL || 
       $white_space === WHITESPACE_PRE_WRAP || 
       $white_space === WHITESPACE_PRE_LINE) && 
      $nowrap === NOWRAP_NORMAL;
  }

  function get_left_background()   { return $this->get_left_padding();   }
  function get_right_background()  { return $this->get_right_padding();  }
  function get_top_background()    { return $this->get_top_padding();    }
  function get_bottom_background() { return $this->get_bottom_padding(); }

  function isVisibleInFlow() { 
    $visibility = $this->getCSSProperty(CSS_VISIBILITY);
    $position   = $this->getCSSProperty(CSS_POSITION);

    return 
      $visibility === VISIBILITY_VISIBLE &&
      $position !== POSITION_FIXED;
  }

  function reflow_footnote(&$parent, &$context) {
    $this->reflow_static($parent, $context);
  }

  /**
   * The  'top'  and 'bottom'  properties  move relatively  positioned
   * element(s) up  or down without  changing their size.  'top' moves
   * the boxes down,  and 'bottom' moves them up.  Since boxes are not
   * split or stretched as a result of 'top' or 'bottom', the computed
   * values  are always:  top =  -bottom.  If both  are 'auto',  their
   * computed  values are  both  '0'. If  one  of them  is 'auto',  it
   * becomes the negative of the other. If neither is 'auto', 'bottom'
   * is ignored  (i.e., the computed  value of 'bottom' will  be minus
   * the value of 'top').
   */
  function offsetRelative() {
    /**
     * Note  that  percentage   positioning  values  are  ignored  for
     * relative positioning
     */

    /**
     * Check if 'top' value is percentage
     */
    $top = $this->getCSSProperty(CSS_TOP);
    if ($top->isNormal()) {
      $top_value = $top->getPoints();
    } elseif ($top->isPercentage()) {
      $containing_block = $this->_get_containing_block();
      $containing_block_height = $containing_block['top'] - $containing_block['bottom'];
      $top_value = $containing_block_height * $top->getPercentage() / 100;
    } elseif ($top->isAuto()) {
      $top_value = null;
    }

    /**
     * Check if 'bottom' value is percentage
     */
    $bottom = $this->getCSSProperty(CSS_BOTTOM);
    if ($bottom->isNormal()) {
      $bottom_value = $bottom->getPoints();
    } elseif ($bottom->isPercentage()) {
      $containing_block = $this->_get_containing_block();
      $containing_block_height = $containing_block['top'] - $containing_block['bottom'];
      $bottom_value = $containing_block_height * $bottom->getPercentage() / 100;
    } elseif ($bottom->isAuto()) {
      $bottom_value = null;
    }

    /**
     * Calculate vertical offset for relative positioned box
     */
    if (!is_null($top_value)) {
      $vertical_offset = -$top_value;
    } elseif (!is_null($bottom_value)) {
      $vertical_offset = $bottom_value;
    } else {
      $vertical_offset = 0;
    };

    /**
     * Check if 'left' value is percentage
     */
    $left = $this->getCSSProperty(CSS_LEFT);
    if ($left->isNormal()) {
      $left_value = $left->getPoints();
    } elseif ($left->isPercentage()) {
      $containing_block = $this->_get_containing_block();
      $containing_block_width = $containing_block['right'] - $containing_block['left'];
      $left_value = $containing_block_width * $left->getPercentage() / 100;
    } elseif ($left->isAuto()) {
      $left_value = null;
    }

    /**
     * Check if 'right' value is percentage
     */
    $right = $this->getCSSProperty(CSS_RIGHT);
    if ($right->isNormal()) {
      $right_value = $right->getPoints();
    } elseif ($right->isPercentage()) {
      $containing_block = $this->_get_containing_block();
      $containing_block_width = $containing_block['right'] - $containing_block['left'];
      $right_value = $containing_block_width * $right->getPercentage() / 100;
    } elseif ($right->isAuto()) {
      $right_value = null;
    }

    /**
     * Calculate vertical offset for relative positioned box
     */
    if (!is_null($left_value)) {
      $horizontal_offset = $left_value;
    } elseif (!is_null($right_value)) {
      $horizontal_offset = -$right_value;
    } else {
      $horizontal_offset = 0;
    };

    $this->offset($horizontal_offset, 
                  $vertical_offset);
  }
}
?>