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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
/add-child {
  1 index /add-child call-method
} def

/box-container-add-child {         % => Child This
  dup 2 index put-parent           % => Child This
  
  1 index
  1 index get-content              % => Child This Child Content
  array-prepend                    % => Child This Content'
  1 index put-content

  pop pop
} def

/box-container-add-deferred-float {% => Float This
  dup /DeferredFloats get          % => Float This DeferredFloats
  2 index exch array-prepend       % => Float This Floats
  1 index exch
  /DeferredFloats exch
  put                              % => Float This
  pop pop
} def

/box-container-append-line {       % => Child This
  exch 
  1 index get-line                 % => Parent Child PLine
  array-prepend   
  exch put-line
} def

/box-container-calculate-text-indent {
                                   % => This
  dup /text-indent get-css-value   % => This TextIndent

  dup /Percentage get {
    /Value get
    1 index /get-width call-method mul 100 div
  } {
    /Value get
  } ifelse                         % => This Value

  exch pop
} def

/box-container-clear-deferred-floats {
  get-box-dict [] /DeferredFloats put
} def

/box-container-clear-line {        % => Box
  [] exch put-line
} def

/box-container-close-line {        % => Context This
  false 2 index 2 index
  box-container-close-line-common  % => Context This
  pop pop 
} def

/box-container-close-last-line {   % => Context This
  true 2 index 2 index
  box-container-close-line-common  % => Context This
  pop pop 
} def

/box-container-close-line-common { % => IsLastLine Context This
% Align line-box using 'text-align' property

% 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
  dup get-line dup length 0 gt {   % => IsLastLine Context This Line
    0 get is-inline {              % => IsLastLine Context This
      dup /text-align get-css-value
      3 index exch                 % => IsLastLine Context This IsLastLine AlignFun
      3 index exch                 % => IsLastLine Context This IsLastLine Context AlignFun
      3 index exch                 % => IsLastLine Context This IsLastLine Context This AlignFun
      cvx exec                     % => IsLastLine Context This 
    } {                            % => IsLastLine Context This
% Nevertheless, CENTER tag and P/DIV with ALIGN attribute set should affect the 
% position of non-inline children.
      dup /pseudo-align get-css-value
      3 index exch                 % => IsLastLine Context This IsLastLine AlignFun
      3 index exch                 % => IsLastLine Context This IsLastLine Context AlignFun
      3 index exch                 % => IsLastLine Context This IsLastLine Context This AlignFun
      cvx exec
    } ifelse                       % => IsLastLine Context This
  } {
    pop
  } ifelse                         % => IsLastLine Context This

% 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

  0 0                              % => IsLastLine Context This 0(Baseline) 0(Height)
  2 index get-line {               % => IsLastLine Context This 0(Baseline) 0(Height) Child
    dup /vertical-align get-css-value
    /baseline eq {                 % => IsLastLine Context This Baseline Height Child
      dup get-default-baseline     % => IsLastLine Context This Baseline Height Child DefaultDaseline
      4 3 roll                     % => IsLastLine Context This Height Child DefaultDaseline Baseline
      max                          % => IsLastLine Context This Height Child Baseline'
      3 1 roll                     % => IsLastLine Context This Baseline' Height Child 
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline' Height 

  2 index get-line {               % => IsLastLine Context This Baseline' Height Child
    dup /vertical-align get-css-value
    /baseline eq {                 % => IsLastLine Context This Baseline Height Child
      2 index 1 index put-baseline % => IsLastLine Context This Baseline Height Child

      dup get-baseline-offset
      1 index get-full-height add  % => IsLastLine Context This Baseline Height Child H+BO
      3 2 roll                     % => IsLastLine Context This Baseline Child Height H+BO
      max                          % => IsLastLine Context This Baseline Child Height'
      exch                         % => IsLastLine Context This Baseline Height' Child
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline Height

% SUB vertical align
  2 index get-line {               % => IsLastLine Context This Baseline' Height Child
    dup /vertical-align get-css-value
    /sub eq {                      % => IsLastLine Context This Baseline Height Child
      2 index
      1 index get-full-height
      2 div                        % => IsLastLine Context This Baseline Height Child
      add
      1 index put-baseline
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline Height

% SUPER vertical align
  2 index get-line {               % => IsLastLine Context This Baseline' Height Child
    dup /vertical-align get-css-value
    /super eq {                    % => IsLastLine Context This Baseline Height Child
      dup get-full-height 2 div    % => IsLastLine Context This Baseline Height Child BL
      1 index put-baseline
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline Height

% MIDDLE vertical align
  0                                % => IsLastLine Context This Baseline Height 0
  3 index get-line {               % => IsLastLine Context This Baseline Height Middle Child
    dup /vertical-align get-css-value
    /middle eq {                   % => IsLastLine Context This Baseline Height Middle Child
      dup get-full-height 2 div    % => IsLastLine Context This Baseline Height Middle Child Middle
      3 2 roll                     % => IsLastLine Context This Baseline Height Child Middle Middle2
      max                          % => IsLastLine Context This Baseline Height Child Middle'
      exch                         % => IsLastLine Context This Baseline Height Middle Child 
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline Height Middle

  dup 2 mul 2 index gt {           % => IsLastLine Context This Baseline Height Middle
    dup 2 index 2 div sub          % => IsLastLine Context This Baseline Height Middle Delta
% offset already aligned items
    4 index get-line {             % => IsLastLine Context This Baseline Height Middle Delta Child
      dup get-baseline
      2 index add
      1 index put-baseline   
      pop
    } forall                       % => IsLastLine Context This Baseline Height Middle Delta
    pop                            % => IsLastLine Context This Baseline Height Middle
    2 mul exch pop
  } {
    pop
  } ifelse                         % => IsLastLine Context This Baseline Height

  2 index get-line {               % => IsLastLine Context This Baseline Height Child
    dup /vertical-align get-css-value
    /middle eq {                   % => IsLastLine Context This Baseline Height Child
      dup get-default-baseline
      1 index get-full-height 2 div
      sub
      2 index 2 div
      add
      1 index put-baseline         % => IsLastLine Context This Baseline Height Child
    } if
    pop    
  } forall
  
% BOTTOM vertical align
  0                                % => IsLastLine Context This Baseline Height 0
  3 index get-line {               % => IsLastLine Context This Baseline Height Bottom Child
    dup /vertical-align get-css-value
    /bottom eq {                   % => IsLastLine Context This Baseline Height Bottom Child
      dup get-full-height          % => IsLastLine Context This Baseline Height Bottom Child Bottom
      3 2 roll                     % => IsLastLine Context This Baseline Height Child Bottom Bottom2
      max                          % => IsLastLine Context This Baseline Height Child Bottom'
      exch                         % => IsLastLine Context This Baseline Height Bottom Child 
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline Height Bottom

  dup 2 index gt       {           % => IsLastLine Context This Baseline Height Bottom
    dup 2 index sub                % => IsLastLine Context This Baseline Height Bottom Delta
% offset already aligned items
    4 index get-line {             % => IsLastLine Context This Baseline Height Bottom Delta Child
      dup get-baseline
      2 index add
      1 index put-baseline   
      pop
    } forall                       % => IsLastLine Context This Baseline Height Bottom Delta
    pop                            % => IsLastLine Context This Baseline Height Bottom
    exch pop
  } {
    pop
  } ifelse                         % => IsLastLine Context This Baseline Height

  2 index get-line {               % => IsLastLine Context This Baseline Height Child
    dup /vertical-align get-css-value
    /bottom eq {                   % => IsLastLine Context This Baseline Height Child
      dup get-default-baseline
      1 index get-full-height 
      sub
      2 index 
      add
      1 index put-baseline         % => IsLastLine Context This Baseline Height Child
    } if
    pop    
  } forall

% TOP vertical align
  0                                % => IsLastLine Context This Baseline Height 0
  3 index get-line {               % => IsLastLine Context This Baseline Height Bottom Child
    dup /vertical-align get-css-value
    /top eq {                      % => IsLastLine Context This Baseline Height Bottom Child
      dup get-full-height          % => IsLastLine Context This Baseline Height Bottom Child Bottom
      3 2 roll                     % => IsLastLine Context This Baseline Height Child Bottom Bottom2
      max                          % => IsLastLine Context This Baseline Height Child Bottom'
      exch                         % => IsLastLine Context This Baseline Height Bottom Child 
    } if
    pop
  } forall                         % => IsLastLine Context This Baseline Height Bottom

  dup 2 index gt       {           % => IsLastLine Context This Baseline Height Bottom
    dup 2 index sub                % => IsLastLine Context This Baseline Height Bottom Delta
% offset already aligned items
    4 index get-line {             % => IsLastLine Context This Baseline Height Bottom Delta Child
      dup get-baseline
      2 index add
      1 index put-baseline   
      pop
    } forall                       % => IsLastLine Context This Baseline Height Bottom Delta
    pop                            % => IsLastLine Context This Baseline Height Bottom
    exch pop
  } {
    pop
  } ifelse                         % => IsLastLine Context This Baseline Height

  2 index get-line {               % => IsLastLine Context This Baseline Height Child
    dup /vertical-align get-css-value
    /top eq {                      % => IsLastLine Context This Baseline Height Child
      dup get-default-baseline
      1 index put-baseline         % => IsLastLine Context This Baseline Height Child
    } if
    pop    
  } forall                         % => IsLastLine Context This Baseline Height

  pop pop                          % => IsLastLine Context This

% Calculate the bottom Y coordinate of last line box

  dup get-current-y                % => IsLastLine Context This BY
  1 index get-line {               % => IsLastLine Context This BY Child
% 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

    dup get-bottom-margin
    4 index context-get-collapsed-margin add
    3 index box-generic-extend-height

    get-bottom-margin              % => IsLastLine Context This BY BM
    min                            % => IsLastLine Context This BY'
  } forall

% Clear the line box
  1 index box-container-clear-line

% Reset current X coordinate to the far left
  1 index get-left
  2 index put-current-x
    
% Extend Y coordinate
  1 index put-current-y            % => IsLastLine Context This
  
% Render the deferred floats
  dup box-container-get-deferred-floats {
                                   % => IsLastLine Context This Float
    3 copy                         % => IsLastLine Context This Float Context This Float
    box-container-reflow-static-float
    pop
  } forall                         % => IsLastLine Context This
  dup box-container-clear-deferred-floats

%  modify the current-x value, so that next inline box will not intersect any floating boxes
  dup get-current-y
  1 index get-current-x            % => IsLastLine Context This Y X 
  3 index context-float-left-x     % => IsLastLine Context This X

  1 index put-current-x            % => IsLastLine Context This

% clear the stack
  pop pop pop
} def

/box-container-create {            % =>
  box-generic-create

  dup /Content [] put
  dup /Line [] put
  
  dup box-container-setup-methods
  dup /box-container add-type
} def

% 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
%
/box-container-get-available-width { % => Context Box
  dup get-current-y
  1 index get-left
  3 index context-float-left-x     % => Context Box FL
  1 index get-left sub             % => Context Box FloatLeftWidth

  1 index get-right
  2 index get-current-y
  3 index get-right
  5 index context-float-right-x
  sub                              % => Context Box FloatLeftWidth FloatRightWidth

  2 index /get-width call-method
  exch sub
  exch sub

  exch pop
  exch pop
} def

% Get a list of floating boxes which shold be rendered
% at the end of current line box
%
/box-container-get-deferred-floats {
  get-box-dict /DeferredFloats get
} def

% 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 NullBoxPDF is treated as null box)
%
% @return reference to the first visible child of current box 
/box-container-get-first {         % => This
  /null
  1 index get-content {            % => This /null Child
    dup /is-whitespace call-method not
    1 index /is-null call-method not
    and {                          % => This FC Child
      exch pop                     % => This Child
      exit
    } if                           % => This FC Child
    pop
  } forall                         % => This FC

  exch pop                         % => FC
} def

% Get first text or image 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 NullBoxPDF is treated as null box)
%
% @return reference to the first visible child of current box 
/box-container-get-first-data {    % => This
  /null
  1 index get-content {            % => This /null Child
    dup /is-whitespace call-method not
    1 index /is-null call-method not
    and {                          % => This FC Child
      dup is-container {
        box-container-get-first-data
        dup /null ne {
          exch pop exit
        } if
      } {
        exch pop exit
      } ifelse                     % => This FC Child
    } if                           % => This FC Child
    pop
  } forall                         % => This FC

  exch pop                         % => FC
} def

% 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 NullBoxPDF is treated as null box)
%
% @return reference to the first visible child of current box
/box-container-get-last {          % => This
  /null
  1 index get-content              % => This /null Content
  dup length 1 sub -1 0 {          % => This /null Content I
    2 copy get                     % => This /null Context I Element
    
    dup /is-whitespace call-method not
    1 index /is-null call-method not
    and
    {
      4 3 roll pop 3 1 roll        % => This /null Context I
      pop
      exit
    } {
      pop
    } ifelse                       % => This /null Context I
    pop
  } for                            % => This Last Content 
  pop exch pop 
} def


/box-container-get-max-width {     % => Context This
  0                                % => Context This MaxW

% We need to add text indent to the max width
  1 index
  box-generic-calc-text-indent     % => Context This MaxW CMaxW

  2 index get-content {            % => Context This MaxW CMaxW Item
    dup is-inline
    1 index /float get-css-value
    /none ne or {                  % => Context This MaxW CMaxW Item
      4 index
      1 index
      /get-max-width call-method   % => Context This MaxW CMaxW Item W
      3 2 roll add exch            % => Context This MaxW CMaxW' Item 
    } {                            % => Context This MaxW CMaxW Item
      2 index 2 index max          % => Context This MaxW CMaxW Item MaxW'
      4 3 roll pop                 % => Context This CMaxW Item MaxW'
      3 1 roll                     % => Context This MaxW CMaxW Item 
      
      4 index
      1 index /get-max-width
      call-method                  % => Context This MaxW CMaxW Item CMaxW'
      3 2 roll pop exch            % => Context This MaxW CMaxW' Item

% Process special case with percentage constrained table

      dup get-width-constraint     % => Context This MaxW CMaxW Item WC 
      
      1 index /box-table is-a
      1 index /type get
      /fraction eq and {           % => Context This MaxW CMaxW Item WC
        4 index
        box-generic-get-expandable-width % => Context This MaxW CMaxW Item WC ExpandableWidth

        5 index
        /get-width call-method     % => Context This MaxW CMaxW Item WC ExpandableWidth Width

        2 index
        dup /apply get exec        % => Context This MaxW CMaxW Item WC CWidth

        3 index max                % => Context This MaxW CMaxW Item WC CMaxW'

        4 3 roll pop 3 1 roll      % => Context This MaxW CMaxW' Item WC 
      } if                         % => Context This MaxW CMaxW Item WC
      pop                          % => Context This MaxW CMaxW Item
    } ifelse                       % => Context This MaxW CMaxW Item 

    pop
  } forall                         % => Context This MaxW CMaxW

% Check if last line have maximal width

  max                              % => Context This MaxW

% Note that max width cannot differ drom constrained width,
% if any width constraints apply

  1 index get-width-constraint     % => Context This MaxW WC
  dup /type get /none ne {
    2 index get-parent
    /get-width call-method         % => Context This MaxW WC PWidth
    2 index                        % => Context This MaxW WC PWidth MaxW
    2 index dup  /apply get exec   % => Context This MaxW WC MaxW'
    exch pop                       % => Context This MaxW MaxW'
    exch pop
  } {
    pop
  } ifelse                         % => Context This MaxW

  1 index get-hor-extra
  add

  exch pop
  exch pop
} def

/box-container-get-min-nowrap-width { % => Context This
  0                                % => Context This MaxW
  
% We need to add text indent to the width

  1 index
  box-generic-calc-text-indent     % => Context This MaxW CMaxW

  2 index get-content {            % => Context This MaxW CMaxW Child
    dup is-inline {
% Inline boxes content will not be wrapped, so we may calculate its max width
      4 index exch
      /get-max-width call-method   % => Context This MaxW CMaxW CMW
      add                          % => Context This MaxW CMaxW 
    } {                            % => Context This MaxW CMaxW Child
      3 1 roll                     % => Context This Child MaxW CMaxW
      max                          % => Context This Child MaxW'
      exch                         % => Context This MaxW' Child
      3 index exch                 % => Context This MaxW' Context Child
      /get-min-width call-method   % => Context This MaxW' CMaxW'
    } ifelse                       % => Context This MaxW CMaxW 
  } forall                         % => Context This MaxW CMaxW

% Check if last line have maximal width
  max                              % => Context This MaxW

% Apply width constraint to min width. Return maximal value
  1 index get-parent
  /get-width call-method           % => Context This MaxW ParentW
  1 index                          % => Context This MaxW ParentW W
  3 index get-width-constraint     % => Context This MaxW ParentW W WC
  dup /apply get exec              % => Context This MaxW MaxW'
  max                              % => Context This MaxW

  1 index get-hor-extra add        % => Context This MaxW
  exch pop
  exch pop
} def

/box-container-get-min-width {     % => Context This
% If box does not have any context, its minimal width is determined by extra horizontal space
  dup get-content length 0 eq {    % => Context This
    dup get-hor-extra
  } {                              % => Context This
% If we're in 'nowrap' mode, minimal and maximal width will be equal
    dup /white-space get-css-value /nowrap eq
    1 index
    /pseudo-nowrap get-css-value
    /nowrap eq or {                % => Context This
      2 copy box-container-get-min-nowrap-width
    } {                            % => Context This
% We need to add text indent size to the with of the first item
      dup
      box-generic-calc-text-indent % => Context This TI
      2 index
      2 index                      % => Context This TI Context This
      get-content 0 get
%      box-container-get-first-data % => Context This TI Context First 
      /get-min-width call-method   % => Context This TI WFirst
      add                          % => Context This MinW

      1 index get-content {        % => Context This MinW Child
        3 index exch
        /get-min-width call-method % => Context This MinW CMinW
        max                        % => Context This MinW
      } forall                     % => Context This MinW 

      1 index get-parent
      /get-width call-method       % => Context This MinW ParentW
      1 index                      % => Context This MinW ParentW MinW
      3 index get-width-constraint % => Context This MinW ParentW MinW WC
      dup /apply get exec          % => Context This MinW MinW'
      max
      
      1 index get-hor-extra add    % => Context This MinW'
    } ifelse                       % => Context This Width
  } ifelse                         % => Context This Width

  exch pop
  exch pop
} def

% 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 
/box-container-get-real-full-height { % => This
% Treat items with overflow: hidden specifically, 
% as floats flown out of this boxes will not be visible
  dup /overflow get-css-value
  /hidden eq {                     % => This
    dup get-full-height            % => This Height
  } {                              % => This
% Check if this cell is totally empty
    dup get-content length 0 eq {
      0                            % => This Height
    } {                            % => This
% Initialize the vertical extent taken by content using the 
% very first child
      dup get-content 0 get
      dup get-top-margin           % => This C0 Top
      1 index get-bottom-margin    % => This C0 Top Bottom
      3 2 roll pop                 % => This Top Bottom

      2 index get-content {        % => This Top Bottom Child
% Check if top margin of current child is to the up 
% of vertical extent top margin
        dup get-top-margin         % => This Top Bottom Child ChildTop
        4 3 roll max 3 1 roll      % => This Top' Bottom Child

% 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
        dup /overflow get-css-value
        /hidden eq {
          dup get-bottom-margin
          3 2 roll min
          exch                     % => This Top Bottom Child
        } {
          dup get-bottom-margin
          1 index get-top-margin
          2 index /get-real-full-height call-method
          sub
          min                      % => This Top Bottom Child ChildBM
          3 2 roll min exch        % => This Top Bottom' Child 
        } ifelse                   % => This Top Bottom Child
        pop
      } forall                     % => This Top Bottom
      sub 0 max                    % => This RH
      1 index get-vert-extra add   % => This Height
    } ifelse
  } ifelse                         % => This Height
  exch pop
} def

/box-container-is-first {          % => Box This
  dup box-container-get-first      % => Box This First
  dup /null eq {
    pop false
  } {
    get-uid
    2 index get-uid
    eq
  } ifelse

  exch pop
  exch pop
} def

% 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
%
/box-container-line-box-empty {    % => This
  dup get-line length 0 eq {
    pop true                       % => true
  } {
    true 1 index get-line          % => This true Line
    {                              % => This Flag Child
      /is-whitespace call-method
      and
    } forall

    exch pop
  } ifelse
} def

/box-container-offset {            % => DY DX This
  3 copy box-generic-offset

  dup get-current-x
  2 index add
  1 index put-current-x

  dup get-current-y
  3 index add
  1 index put-current-y
  
  dup get-content {
    3 index exch
    3 index exch
    /offset call-method
  } forall

  pop pop pop
} def

/box-container-offset-if-first {   % => DY DX Box This
  2 copy box-container-is-first {
    dup get-parent /null ne {
      3 index
      3 index
      3 index
      3 index get-parent          % => DY DX Box This DY DX Box Parent

      box-container-offset-if-first
                                  % => DY DX Box This ParentResult

      not {
        3 index
        3 index
        2 index /offset call-method  % => DY DX Box This
        true
      } { false } ifelse
    } { false } ifelse
  } { false }ifelse

  exch pop
  exch pop
  exch pop
  exch pop
} def

/box-container-pre-reflow-images { % => This
  dup get-content {
    /pre-reflow-images call-method
  } forall
  pop
} def

/box-container-setup-methods {     % => Box
  dup get-box-dict /Methods get    % => Box Methods
  dup /add-child            { box-container-add-child } put
  dup /get-max-width        { box-container-get-max-width } put
  dup /get-min-width        { box-container-get-min-width } put
  dup /get-real-full-height { box-container-get-real-full-height } put
  dup /offset               { box-container-offset } put
  dup /pre-reflow-images    { box-container-pre-reflow-images } put
  dup /reflow-anchors       { box-container-reflow-anchors } put
  dup /reflow-content       { box-container-reflow-content } put
  dup /reflow-inline        { box-container-reflow-inline } put
  dup /reflow-static-float  { box-container-reflow-static-float } put
  dup /show                 { box-container-show } put
  pop pop
} def

/box-container-show {              % => Viewport Box
  2 copy box-generic-show          % => Viewport Box

% Setup clipping path for non 'overflow: visible' boxes
  dup /overflow get-css-value
  /visible ne {
% Save clipping area state (of course, BEFORE the clipping area will be set)
    gsave

    newpath
    dup get-left-border
    1 index get-bottom-border      % => Viewport Box X Y
    2 index get-right-border
    3 index get-left-border sub    % => Viewport Box X Y W
    3 index get-top-border
    4 index get-bottom-border sub   % => Viewport Box X Y W H
    rectclip
    
  } if
  
% draw content
  dup get-content {                % => Viewport Box ContentElement
% 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

    dup /visibility get-css-value /visible eq {
      2 index
      1 index
      /show call-method
    } if                           % => Viewport Box ContentElement
    pop
  } forall                         % => Viewport Box 

  dup /overflow get-css-value /visible ne {
% restore previous clipping path
    grestore
  } if

  pop pop
} def                              % => 

/box-container-reflow-anchors {    % => List Viewport Box
  3 copy box-generic-reflow-anchors
    
  dup get-content {                % => List Viewport Box Child
    3 index exch
    3 index exch                   % => List Viewport Box List Viewport Child
    /reflow-anchors call-method    % => List' Viewport Box
  } forall

  pop pop pop
} def

/box-container-reflow-content {    % => Context This
  2 copy box-container-close-line  % => Context This

% If first child is inline - apply text-indent
  dup box-container-get-first
  dup /null ne {
    dup is-inline {                % => Context This First
      pop
      dup box-container-calculate-text-indent
      1 index get-additional-text-indent
      add
      1 index get-current-x
      add
      1 index put-current-x        % => Context This
    } {
      pop
    } ifelse
  } {
    pop
  } ifelse

  0 1 index put-height

% reset current Y value
  dup get-top
  1 index put-current-y

  dup get-content {                % => Context This Child
    2 index exch                   % => Context This Context Child
    2 index exch                   % => Context This Context This Child
    /reflow call-method            % => Context This 
  } forall                         % => Context This

  1 index 1 index
  box-container-close-last-line    % => Context This

  pop pop                          % => 
} def

/box-container-reflow-inline {     % => This
  dup get-content {                % => This Child
    /reflow-inline call-method     % => This
  } forall
  pop
} def

/box-container-reflow-static-float { % => Context Parent Box
% Defer the float rendering till the next line box
  1 index get-line length 0 gt {
    dup 2 index box-container-add-deferred-float
  } {
      
% Calculate margin values if they have been set as a percentage

    2 copy
    box-generic-calc-percentage-margins

% Calculate width value if it have been set as a percentage

    3 copy
    box-generic-calc-percentage-width

% Calculate margins and/or width is 'auto' values have been specified

    2 copy
    box-generic-calc-auto-width-margins

% Determine the actual width of the floating box
% Note that get_max_width returns both content and extra width

    2 index 1 index /get-max-width call-method
    1 index put-full-width

% We need to call this function before determining the horizontal coordinate
% as after vertical offset the additional space to the left may apperar
                                   % => Context Parent Box
    2 index
    2 index get-current-y
    2 index
    box-generic-apply-clear        % => Context Parent Box Y

% determine the position of top-left floating box corner
    1 index /float get-css-value
    /right eq {
      2 index
      2 index get-full-width
      2 index                      % => Context Parent Box Y Parent Width Y
      6 index
      context-float-right-xy       % => Context Parent Box Y X' Y'

      exch
      3 index get-full-width sub
      exch
    } {
      2 index
      2 index get-full-width
      2 index                      % => Context Parent Box Y Parent Width Y
      6 index
      context-float-left-xy        % => Context Parent Box Y X' Y'
    } ifelse                       % => Context Parent Box Y X' Y'

    3 index get-extra-top sub
    exch
    3 index get-extra-left add
    3 index box-generic-move-to    % => Context Parent Box Y
    pop                            % => Context Parent Box

% Reflow contents. 
% Note that floating box creates a new float flow context for it children.
   
    2 index context-push-floats

% Floating box create a separate margin collapsing context

    0 3 index context-push-collapsed-margin

    2 index 1 index /reflow-content call-method

    2 index context-pop-collapsed-margin

% Float should completely enclose its child floats

    2 index context-float-bottom   % => Context Parent Box FB
    dup /null ne {
      1 index box-generic-extend-height
    } {
      pop
    } ifelse
    
    2 index context-float-right    % => Context Parent Box FR
    dup /null ne {
      1 index box-generic-extend-width
    } {
      pop
    } ifelse

% restore old float flow context

    2 index context-pop-floats

% Add this  box to the list of floats in current context

    dup 3 index context-add-float  % => Context Parent Box

% 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

    1 index get-current-y
    2 index get-current-x
    4 index
    context-float-left-x
    2 index put-current-x
    
  } ifelse                         % => Context Parent Box
  pop pop pop
} def

/get-content {
  /Content get
} def

/get-line {
  /Line get
} def

/line-length {                     % => Box 
  dup get-line 0 exch              % => Box 0(CurrentLength) Line
  {                                % => Box CurrentLength LineElement
% 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
    dup get-full-width             % => Box CurrentLength LineElement EWidth
    3 2 roll add exch              % => Box CurrentLength LineElement

    dup get-parent                 % => Box CurrentLength LineElement EParent
    dup /null ne {
      dup box-container-get-first  % => Box CurrentLength LineElement EParent First
      1 index
      box-container-get-last       % => Box CurrentLength LineElement EParent First Last

      1 index /null ne {
        1 index get-uid
        4 index get-uid eq {
          2 index
          /get-extra-line-left
          call-method              % => Box CurrentLength LineElement EParent First Last ELeft
          6 5 roll add 5 1 roll    % => Box CurrentLength' LineElement EParent First Last 
        } if
      } if

      dup /null ne {
        dup get-uid
        4 index get-uid eq {
          2 index
          /get-extra-line-right
          call-method              % => Box CurrentLength LineElement EParent First Last ELeft
          6 5 roll add 5 1 roll    % => Box CurrentLength' LineElement EParent First Last 
        } if
      } if

      pop pop                      % => Box CurrentLength LineELement EParent
    } if                           % => Box CurrentLength LineElement EParent
    pop pop
  } forall                         % => Box CurrentLength
  exch pop                         % => Length
} def

/put-line {
  exch /Line exch put
} def