@@ -55,6 +55,8 @@ public class MulticolRenderer extends AbstractRenderer {
5555 private Float heightFromProperties ;
5656 private float columnGap ;
5757
58+ private boolean isFirstLayout = true ;
59+
5860 /**
5961 * Creates a DivRenderer from its corresponding layout object.
6062 *
@@ -84,6 +86,8 @@ public LayoutResult layout(LayoutContext layoutContext) {
8486 Rectangle actualBBox = layoutContext .getArea ().getBBox ().clone ();
8587 float originalWidth = actualBBox .getWidth ();
8688 applyWidth (actualBBox , originalWidth );
89+
90+ ContinuousContainer .setupContinuousContainerIfNeeded (this );
8791 applyPaddings (actualBBox , false );
8892 applyBorderBox (actualBBox , false );
8993 applyMargins (actualBBox , false );
@@ -103,6 +107,12 @@ public LayoutResult layout(LayoutContext layoutContext) {
103107 if (layoutResult .getSplitRenderers ().isEmpty ()) {
104108 return new LayoutResult (LayoutResult .NOTHING , null , null , this , layoutResult .getCauseOfNothing ());
105109 } else if (layoutResult .getOverflowRenderer () == null ) {
110+ final ContinuousContainer continuousContainer = this .<ContinuousContainer >getProperty (
111+ Property .TREAT_AS_CONTINUOUS_CONTAINER_RESULT );
112+ if (continuousContainer != null ) {
113+ continuousContainer .reApplyProperties (this );
114+ }
115+
106116 this .childRenderers .clear ();
107117 addAllChildRenderers (layoutResult .getSplitRenderers ());
108118 this .occupiedArea = calculateContainerOccupiedArea (layoutContext , true );
@@ -201,7 +211,8 @@ protected AbstractRenderer createSplitRenderer(List<IRenderer> children) {
201211 * @return a new {@link AbstractRenderer} instance
202212 */
203213 protected AbstractRenderer createOverflowRenderer (IRenderer overflowedContentRenderer ) {
204- AbstractRenderer overflowRenderer = (AbstractRenderer ) getNextRenderer ();
214+ MulticolRenderer overflowRenderer = (MulticolRenderer ) getNextRenderer ();
215+ overflowRenderer .isFirstLayout = false ;
205216 overflowRenderer .parent = parent ;
206217 overflowRenderer .modelElement = modelElement ;
207218 overflowRenderer .addAllProperties (getOwnProperties ());
@@ -260,9 +271,10 @@ private Float determineHeight(Rectangle parentBBox) {
260271 }
261272
262273
263- private void recalculateHeightWidthAfterLayouting (Rectangle parentBBox ) {
274+ private void recalculateHeightWidthAfterLayouting (Rectangle parentBBox , boolean isFull ) {
264275 Float height = determineHeight (parentBBox );
265276 if (height != null ) {
277+ height = updateOccupiedHeight ((float ) height , isFull );
266278 float heightDelta = parentBBox .getHeight () - (float ) height ;
267279 parentBBox .moveUp (heightDelta );
268280 parentBBox .setHeight ((float ) height );
@@ -298,13 +310,6 @@ private MulticolLayoutResult balanceContentAndLayoutColumns(LayoutContext prelay
298310 float workingHeight = approximateHeight ;
299311 if (heightFromProperties != null ) {
300312 workingHeight = Math .min ((float ) heightFromProperties , (float ) approximateHeight );
301- workingHeight -= safelyRetrieveFloatProperty (Property .PADDING_TOP );
302- workingHeight -= safelyRetrieveFloatProperty (Property .PADDING_BOTTOM );
303- workingHeight -= safelyRetrieveFloatProperty (Property .BORDER_TOP );
304- workingHeight -= safelyRetrieveFloatProperty (Property .BORDER_BOTTOM );
305- workingHeight -= safelyRetrieveFloatProperty (Property .BORDER ) * 2 ;
306- workingHeight -= safelyRetrieveFloatProperty (Property .MARGIN_TOP );
307- workingHeight -= safelyRetrieveFloatProperty (Property .MARGIN_BOTTOM );
308313 }
309314 result = layoutColumnsAndReturnOverflowRenderer (prelayoutContext , actualBbox , workingHeight );
310315
@@ -371,29 +376,43 @@ private void clearOverFlowRendererIfNeeded(MulticolLayoutResult result) {
371376
372377 private LayoutArea calculateContainerOccupiedArea (LayoutContext layoutContext , boolean isFull ) {
373378 LayoutArea area = layoutContext .getArea ().clone ();
374- float totalHeight = approximateHeight ;
375-
376- if (isFull ) {
377- totalHeight += safelyRetrieveFloatProperty (Property .PADDING_BOTTOM );
378- totalHeight += safelyRetrieveFloatProperty (Property .MARGIN_BOTTOM );
379- totalHeight += safelyRetrieveFloatProperty (Property .BORDER_BOTTOM );
380- }
381- totalHeight += safelyRetrieveFloatProperty (Property .PADDING_TOP );
382-
383- totalHeight += safelyRetrieveFloatProperty (Property .MARGIN_TOP );
384379
385- totalHeight += safelyRetrieveFloatProperty (Property .BORDER_TOP );
386- final float TOP_AND_BOTTOM = isFull ? 2 : 1 ;
387-
388- totalHeight += safelyRetrieveFloatProperty (Property .BORDER ) * TOP_AND_BOTTOM ;
380+ final float totalHeight = updateOccupiedHeight (approximateHeight , isFull );
389381
390382 area .getBBox ().setHeight (totalHeight );
391383 final Rectangle initialBBox = layoutContext .getArea ().getBBox ();
392384 area .getBBox ().setY (initialBBox .getY () + initialBBox .getHeight () - area .getBBox ().getHeight ());
393- recalculateHeightWidthAfterLayouting (area .getBBox ());
385+ recalculateHeightWidthAfterLayouting (area .getBBox (), isFull );
394386 return area ;
395387 }
396388
389+ private float updateOccupiedHeight (float initialHeight , boolean isFull ) {
390+ if (isFull ) {
391+ initialHeight += safelyRetrieveFloatProperty (Property .PADDING_BOTTOM );
392+ initialHeight += safelyRetrieveFloatProperty (Property .MARGIN_BOTTOM );
393+ if (!this .hasOwnProperty (Property .BORDER ) || this .<Border >getProperty (Property .BORDER ) == null ) {
394+ initialHeight += safelyRetrieveFloatProperty (Property .BORDER_BOTTOM );
395+ }
396+ }
397+ initialHeight += safelyRetrieveFloatProperty (Property .PADDING_TOP );
398+
399+ initialHeight += safelyRetrieveFloatProperty (Property .MARGIN_TOP );
400+
401+ if (!this .hasOwnProperty (Property .BORDER ) || this .<Border >getProperty (Property .BORDER ) == null ) {
402+ initialHeight += safelyRetrieveFloatProperty (Property .BORDER_TOP );
403+ }
404+
405+ // isFirstLayout is necessary to handle the case when multicol container layouted in more
406+ // than 2 pages, and on the last page layout result is full, but there is no bottom border
407+ float TOP_AND_BOTTOM = isFull && isFirstLayout ? 2 : 1 ;
408+ // Multicol container layouted in more than 3 pages, and there is a page where there are no bottom and top borders
409+ if (!isFull && !isFirstLayout ) {
410+ TOP_AND_BOTTOM = 0 ;
411+ }
412+ initialHeight += safelyRetrieveFloatProperty (Property .BORDER ) * TOP_AND_BOTTOM ;
413+ return initialHeight ;
414+ }
415+
397416 private BlockRenderer getElementsRenderer () {
398417 if (!(getChildRenderers ().size () == 1 && getChildRenderers ().get (0 ) instanceof BlockRenderer )) {
399418 throw new IllegalStateException ("Invalid child renderers, it should be one and be a block element" );
0 commit comments