private MarginsCollapseInfo CreateMarginsInfoForBlockChild(int childIndex)
        {
            bool ignoreChildTopMargin = false;
            // always assume that current child might be the last on this area
            bool ignoreChildBottomMargin = LastChildMarginAdjoinedToParent(renderer);

            if (childIndex == firstNotEmptyKidIndex)
            {
                ignoreChildTopMargin = FirstChildMarginAdjoinedToParent(renderer);
            }
            MarginsCollapse childCollapseBefore;

            if (childIndex == 0)
            {
                MarginsCollapse parentCollapseBefore = collapseInfo.GetCollapseBefore();
                childCollapseBefore = ignoreChildTopMargin ? parentCollapseBefore : new MarginsCollapse();
            }
            else
            {
                MarginsCollapse prevChildCollapseAfter = prevChildMarginInfo != null?prevChildMarginInfo.GetOwnCollapseAfter
                                                             () : null;

                childCollapseBefore = prevChildCollapseAfter != null ? prevChildCollapseAfter : new MarginsCollapse();
            }
            MarginsCollapse     parentCollapseAfter = collapseInfo.GetCollapseAfter().Clone();
            MarginsCollapse     childCollapseAfter  = ignoreChildBottomMargin ? parentCollapseAfter : new MarginsCollapse();
            MarginsCollapseInfo childMarginsInfo    = new MarginsCollapseInfo(ignoreChildTopMargin, ignoreChildBottomMargin
                                                                              , childCollapseBefore, childCollapseAfter);

            if (ignoreChildTopMargin && childIndex == firstNotEmptyKidIndex)
            {
                childMarginsInfo.SetBufferSpaceOnTop(collapseInfo.GetBufferSpaceOnTop());
            }
            if (ignoreChildBottomMargin)
            {
                childMarginsInfo.SetBufferSpaceOnBottom(collapseInfo.GetBufferSpaceOnBottom());
            }
            return(childMarginsInfo);
        }
        public virtual void EndMarginsCollapse(Rectangle layoutBox)
        {
            if (backupLayoutBox != null)
            {
                RestoreLayoutBoxAfterFailedLayoutAttempt(layoutBox);
            }
            if (prevChildMarginInfo != null)
            {
                UpdateCollapseBeforeIfPrevKidIsFirstAndSelfCollapsed(prevChildMarginInfo.GetCollapseAfter());
            }
            bool couldBeSelfCollapsing = iText.Layout.Margincollapse.MarginsCollapseHandler.MarginsCouldBeSelfCollapsing
                                             (renderer) && !lastKidCollapsedAfterHasClearanceApplied;
            bool blockHasNoKidsWithContent = collapseInfo.IsSelfCollapsing();

            if (FirstChildMarginAdjoinedToParent(renderer))
            {
                if (blockHasNoKidsWithContent && !couldBeSelfCollapsing)
                {
                    AddNotYetAppliedTopMargin(layoutBox);
                }
            }
            collapseInfo.SetSelfCollapsing(collapseInfo.IsSelfCollapsing() && couldBeSelfCollapsing);
            if (!blockHasNoKidsWithContent && lastKidCollapsedAfterHasClearanceApplied)
            {
                ApplySelfCollapsedKidMarginWithClearance(layoutBox);
            }
            MarginsCollapse ownCollapseAfter;
            bool            lastChildMarginJoinedToParent = prevChildMarginInfo != null && prevChildMarginInfo.IsIgnoreOwnMarginBottom
                                                                () && !lastKidCollapsedAfterHasClearanceApplied;

            if (lastChildMarginJoinedToParent)
            {
                ownCollapseAfter = prevChildMarginInfo.GetOwnCollapseAfter();
            }
            else
            {
                ownCollapseAfter = new MarginsCollapse();
            }
            ownCollapseAfter.JoinMargin(GetModelBottomMargin(renderer));
            collapseInfo.SetOwnCollapseAfter(ownCollapseAfter);
            if (collapseInfo.IsSelfCollapsing())
            {
                if (prevChildMarginInfo != null)
                {
                    collapseInfo.SetCollapseAfter(prevChildMarginInfo.GetCollapseAfter());
                }
                else
                {
                    collapseInfo.GetCollapseAfter().JoinMargin(collapseInfo.GetCollapseBefore());
                    collapseInfo.GetOwnCollapseAfter().JoinMargin(collapseInfo.GetCollapseBefore());
                }
                if (!collapseInfo.IsIgnoreOwnMarginBottom() && !collapseInfo.IsIgnoreOwnMarginTop())
                {
                    float collapsedMargins = collapseInfo.GetCollapseAfter().GetCollapsedMarginsSize();
                    OverrideModelBottomMargin(renderer, collapsedMargins);
                }
            }
            else
            {
                MarginsCollapse marginsCollapseBefore = collapseInfo.GetCollapseBefore();
                if (!collapseInfo.IsIgnoreOwnMarginTop())
                {
                    float collapsedMargins = marginsCollapseBefore.GetCollapsedMarginsSize();
                    OverrideModelTopMargin(renderer, collapsedMargins);
                }
                if (lastChildMarginJoinedToParent)
                {
                    collapseInfo.SetCollapseAfter(prevChildMarginInfo.GetCollapseAfter());
                }
                if (!collapseInfo.IsIgnoreOwnMarginBottom())
                {
                    float collapsedMargins = collapseInfo.GetCollapseAfter().GetCollapsedMarginsSize();
                    OverrideModelBottomMargin(renderer, collapsedMargins);
                }
            }
            if (LastChildMarginAdjoinedToParent(renderer) && (prevChildMarginInfo != null || blockHasNoKidsWithContent
                                                              ))
            {
                // Adjust layout box here in order to make it represent the available area left.
                float collapsedMargins = collapseInfo.GetCollapseAfter().GetCollapsedMarginsSize();
                // May be in case of self-collapsed margins it would make more sense to apply this value to topMargin,
                // because that way the layout box would represent the area left after the empty self-collapsed block, not
                // before it. However at the same time any considerations about the layout (i.e. content) area in case
                // of the self-collapsed block seem to be invalid, because self-collapsed block shall have content area
                // of zero height.
                ApplyBottomMargin(layoutBox, collapsedMargins);
            }
        }