tdf#135035 DOCX import: fix auto width of frame in column

In compatibility mode FRAME_AUTOWIDTH_WITH_MORE_PARA
no frames should be wider as the column it is anchored to,
if the frame width is set to automatic.

If there is a paragraph in the frame, that is longer then
the parent width of frame, then the frame width will be set
to the same size as its parents width.

Co-authored-by: Tibor Nagy (NISZ)

Change-Id: Ie5e7e94fd58219eb944ad9163b1ff2c1e7171858
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102671
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/layout/data/tdf135035.docx b/sw/qa/extras/layout/data/tdf135035.docx
index f314f29..acd7b14 100644
--- a/sw/qa/extras/layout/data/tdf135035.docx
+++ b/sw/qa/extras/layout/data/tdf135035.docx
Binary files differ
diff --git a/sw/qa/extras/layout/data/tdf135035.odt b/sw/qa/extras/layout/data/tdf135035.odt
index 479dab1..bf84ec4 100644
--- a/sw/qa/extras/layout/data/tdf135035.odt
+++ b/sw/qa/extras/layout/data/tdf135035.odt
Binary files differ
diff --git a/sw/qa/extras/layout/layout.cxx b/sw/qa/extras/layout/layout.cxx
index 0084198..5c7bfda 100644
--- a/sw/qa/extras/layout/layout.cxx
+++ b/sw/qa/extras/layout/layout.cxx
@@ -4425,17 +4425,21 @@ CPPUNIT_TEST_FIXTURE(SwLayoutWriter, testTdf135035)
    xmlDocUniquePtr pXmlDoc = parseLayoutDump();
    sal_Int32 nFly1Width = getXPath(pXmlDoc, "(//fly)[1]/infos/prtBounds", "width").toInt32();
    sal_Int32 nFly2Width = getXPath(pXmlDoc, "(//fly)[2]/infos/prtBounds", "width").toInt32();
    sal_Int32 nFly3Width = getXPath(pXmlDoc, "(//fly)[3]/infos/prtBounds", "width").toInt32();
    sal_Int32 nParentWidth = getXPath(pXmlDoc, "(//txt)[1]/infos/prtBounds", "width").toInt32();
    CPPUNIT_ASSERT_EQUAL(nParentWidth, nFly2Width);
    CPPUNIT_ASSERT_EQUAL(nParentWidth, nFly3Width);
    CPPUNIT_ASSERT_LESS(nParentWidth / 2, nFly1Width);

    createDoc("tdf135035.odt");
    pXmlDoc = parseLayoutDump();
    nFly1Width = getXPath(pXmlDoc, "(//fly)[1]/infos/prtBounds", "width").toInt32();
    nFly2Width = getXPath(pXmlDoc, "(//fly)[2]/infos/prtBounds", "width").toInt32();
    nFly3Width = getXPath(pXmlDoc, "(//fly)[3]/infos/prtBounds", "width").toInt32();
    nParentWidth = getXPath(pXmlDoc, "(//txt)[1]/infos/prtBounds", "width").toInt32();
    CPPUNIT_ASSERT_LESS(nParentWidth / 2, nFly2Width);
    CPPUNIT_ASSERT_LESS(nParentWidth / 2, nFly1Width);
    CPPUNIT_ASSERT_GREATER(nParentWidth, nFly3Width);
}

CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/core/layout/fly.cxx b/sw/source/core/layout/fly.cxx
index 3757529..5da3f7e 100644
--- a/sw/source/core/layout/fly.cxx
+++ b/sw/source/core/layout/fly.cxx
@@ -2517,15 +2517,11 @@ static SwTwips lcl_CalcAutoWidth( const SwLayoutFrame& rFrame )
    // No autowidth defined for columned frames
    if ( !pFrame || pFrame->IsColumnFrame() )
        return nRet;
    // tdf#124423 In Microsoft compatibility mode: widen the frame to max (PrintArea of the frame it anchored to) if it contains at least 2 paragraphs.
    if (rFrame.GetFormat()->getIDocumentSettingAccess().get(DocumentSettingId::FRAME_AUTOWIDTH_WITH_MORE_PARA) && pFrame && pFrame->GetNext())
    {
        const SwFrame* pFrameRect = rFrame.IsFlyFrame() ? static_cast<const SwFlyFrame*>(&rFrame)->GetAnchorFrame() : pFrame->FindPageFrame();
        return rFrame.IsVertical() ? pFrameRect->getFramePrintArea().Height() : pFrameRect->getFramePrintArea().Width();
    }

    int nParagraphCount = 0;
    while ( pFrame )
    {
        nParagraphCount++;
        if ( pFrame->IsSctFrame() )
        {
            nMin = lcl_CalcAutoWidth( *static_cast<const SwSectionFrame*>(pFrame) );
@@ -2562,6 +2558,18 @@ static SwTwips lcl_CalcAutoWidth( const SwLayoutFrame& rFrame )
        pFrame = pFrame->GetNext();
    }

    // tdf#124423 In Microsoft compatibility mode: widen the frame to max (PrintArea of the frame it anchored to) if it contains at least 2 paragraphs,
    // or 1 paragraph wider then its parent area.
    if (rFrame.GetFormat()->getIDocumentSettingAccess().get(DocumentSettingId::FRAME_AUTOWIDTH_WITH_MORE_PARA))
    {
        const SwFrame* pFrameRect = rFrame.IsFlyFrame() ? static_cast<const SwFlyFrame*>(&rFrame)->GetAnchorFrame() : rFrame.Lower()->FindPageFrame();
        SwTwips nParentWidth = rFrame.IsVertical() ? pFrameRect->getFramePrintArea().Height() : pFrameRect->getFramePrintArea().Width();
        if (nParagraphCount > 1 || nRet > nParentWidth)
        {
            return nParentWidth;
        }
    }

    return nRet;
}