sw: if the fly is to be split, then limit its growth in SwFlyFrame::Format()

It seems the first direct cause why flys are not split is because flys
can grow forever, so an inner paragraph's SwContentFrame::MakeAll() will
never hit the nBottomDist < 0 condition, which is necessary to call into
SwFlowFrame::MoveFwd().

With this, at least nBottomDist is negative when a fly with two
paragraphs is at the bottom of a page and only one of them fits the body
frame.

Also add a debug environment variable, so I can debug the "fly split"
case till we don't have import/export filters for this.

Towards an initial layout for multi-page fly frames.

Change-Id: I43114b5795dd42e518a1d776ccd2e7ab607ad859
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146376
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
diff --git a/sw/inc/formatflysplit.hxx b/sw/inc/formatflysplit.hxx
index a7cbb82..5f7dda6 100644
--- a/sw/inc/formatflysplit.hxx
+++ b/sw/inc/formatflysplit.hxx
@@ -29,10 +29,7 @@
class SW_DLLPUBLIC SwFormatFlySplit final : public SfxBoolItem
{
public:
    SwFormatFlySplit(bool bSplit = false)
        : SfxBoolItem(RES_FLY_SPLIT, bSplit)
    {
    }
    SwFormatFlySplit(bool bSplit = false);

    SwFormatFlySplit* Clone(SfxItemPool* pPool = nullptr) const override;

diff --git a/sw/source/core/attr/formatflysplit.cxx b/sw/source/core/attr/formatflysplit.cxx
index 72816b2..bcbfcc2 100644
--- a/sw/source/core/attr/formatflysplit.cxx
+++ b/sw/source/core/attr/formatflysplit.cxx
@@ -21,6 +21,15 @@

#include <libxml/xmlwriter.h>

SwFormatFlySplit::SwFormatFlySplit(bool bSplit)
    : SfxBoolItem(RES_FLY_SPLIT, bSplit)
{
    if (getenv("SW_FORCE_FLY_SPLIT"))
    {
        SetValue(true);
    }
}

SwFormatFlySplit* SwFormatFlySplit::Clone(SfxItemPool*) const
{
    return new SwFormatFlySplit(*this);
diff --git a/sw/source/core/layout/fly.cxx b/sw/source/core/layout/fly.cxx
index 4cee6d9..1ece49f 100644
--- a/sw/source/core/layout/fly.cxx
+++ b/sw/source/core/layout/fly.cxx
@@ -75,6 +75,7 @@
#include <bodyfrm.hxx>
#include <FrameControlsManager.hxx>
#include <ndtxt.hxx>
#include <formatflysplit.hxx>

using namespace ::com::sun::star;

@@ -1303,6 +1304,21 @@ void SwFlyFrame::Format( vcl::RenderContext* /*pRenderContext*/, const SwBorderA
            if ( nRemaining < MINFLY )
                nRemaining = MINFLY;

            const SwFrame* pAnchor = GetAnchorFrame();
            const SwFrame* pAnchorUpper = pAnchor ? pAnchor->GetUpper() : nullptr;
            if (pAnchorUpper && GetFormat()->GetFlySplit().GetValue())
            {
                // If the fly is allowed to be split, then limit its size to the upper of the
                // anchor.
                SwTwips nDeadline = aRectFnSet.GetPrtBottom(*pAnchorUpper);
                SwTwips nTop = aRectFnSet.GetTop(getFrameArea());
                SwTwips nBottom = aRectFnSet.GetTop(getFrameArea()) + nRemaining;
                if (nBottom > nDeadline)
                {
                    nRemaining = nDeadline - nTop;
                }
            }

            {
                SwFrameAreaDefinition::FramePrintAreaWriteAccess aPrt(*this);
                aRectFnSet.SetHeight( aPrt, nRemaining );