tdf#136613 sw: fix picture position in tables

in the case of option FollowTextFlow.

Regression from commit b6850bbe95418ecfde404be1696548f18d200c9b
(tdf#106153 sw compatibility: fix textboxes exceeding the page).

Note: now images have the same position than before the previous
commit, but this is still not perfect, because option FollowTextFlow
doesn't allow them to be placed outside the cell where they are
anchored to. The proper fix might be extending FollowTextFlow.

Change-Id: I877e4660cd4a2fd8f1e97c845755940459f1d014
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102910
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/layout/data/tdf136613.docx b/sw/qa/extras/layout/data/tdf136613.docx
new file mode 100644
index 0000000..f885564
--- /dev/null
+++ b/sw/qa/extras/layout/data/tdf136613.docx
Binary files differ
diff --git a/sw/qa/extras/layout/layout.cxx b/sw/qa/extras/layout/layout.cxx
index 4a348bd..ce0a43f 100644
--- a/sw/qa/extras/layout/layout.cxx
+++ b/sw/qa/extras/layout/layout.cxx
@@ -478,6 +478,37 @@ CPPUNIT_TEST_FIXTURE(SwLayoutWriter, TestTdf134272)
    assertXPath(pXmlDoc, "/root/page[1]/header/txt[2]/infos/bounds", "bottom", "2819");
}

CPPUNIT_TEST_FIXTURE(SwLayoutWriter, TestTdf136613)
{
    SwDoc* pDoc = createDoc("tdf136613.docx");
    CPPUNIT_ASSERT(pDoc);

    //get the flys and the root frame
    const auto vFlyFormats = pDoc->GetFlyFrameFormats(FLYCNTTYPE_ALL, true);
    const auto vFrames = pDoc->GetAllLayouts();

    CPPUNIT_ASSERT(!vFrames.empty());
    CPPUNIT_ASSERT(!vFlyFormats.empty());

    //get the page frame from the root
    SwFrame* pPageFrame = vFrames[0]->Lower();
    CPPUNIT_ASSERT(pPageFrame);

    //get the rectangle of the page
    const SwRect& rPageRect = pPageFrame->getFrameArea();

    //check the flys and...
    for (auto pFlyFormat : vFlyFormats)
    {
        //...the rectangle of the fly location...
        const SwRect& rRect = pFlyFormat->FindLayoutRect();
        CPPUNIT_ASSERT(!rRect.IsEmpty());

        //...if it is on the page. This will fail if not.
        CPPUNIT_ASSERT_MESSAGE("The pictures are outside the page!", rPageRect.IsInside(rRect));
    }
}

CPPUNIT_TEST_FIXTURE(SwLayoutWriter, testTdf88496)
{
    SwDoc* pDoc = createDoc("tdf88496.docx");
diff --git a/sw/source/core/objectpositioning/anchoredobjectposition.cxx b/sw/source/core/objectpositioning/anchoredobjectposition.cxx
index d83eaffb..bfcd075 100644
--- a/sw/source/core/objectpositioning/anchoredobjectposition.cxx
+++ b/sw/source/core/objectpositioning/anchoredobjectposition.cxx
@@ -435,8 +435,13 @@ SwTwips SwAnchoredObjectPosition::ImplAdjustVertRelPos( const SwTwips nTopOfAnch
                                                         const bool bCheckBottom ) const
{
    SwTwips nAdjustedRelPosY = nProposedRelPosY;
    if (SwAnchoredObject::IsDraggingOffPageAllowed(FindFrameFormat(&mrDrawObj)))
    // TODO: Replace the following condition with the correction
    // of the implementation of option FollowTextFlow.
    if ( SwAnchoredObject::IsDraggingOffPageAllowed(FindFrameFormat(&mrDrawObj)) &&
        !(GetAnchorFrame().IsInTab() && DoesObjFollowsTextFlow()) )
    {
        return nAdjustedRelPosY;
    }

    const Size aObjSize(GetAnchoredObj().GetObjRect().SSize());
    // determine the area of 'page' alignment frame, to which the vertical