sw/inc/textboxhelper.hxx                     |   39 ++
 sw/qa/extras/ooxmlexport/data/tdf141550.docx |binary
 sw/qa/extras/ooxmlexport/ooxmlexport16.cxx   |   17 +
 sw/qa/extras/uiwriter/data3/tdf140975.docx   |binary
 sw/qa/extras/uiwriter/uiwriter3.cxx          |   30 ++
 sw/qa/uitest/data/tdf141557.docx             |binary
 sw/source/core/doc/textboxhelper.cxx         |  362 +++++++++++++++++++--------
 sw/source/core/text/porfly.cxx               |    1 
 8 files changed, 344 insertions(+), 105 deletions(-)

New commits:
commit bea00ffb1b6d65a276b8bc55a6e0135fb9bcf65b
Author:     Attila Bakos (NISZ) <bakos.attilakar...@nisz.hu>
AuthorDate: Thu Apr 1 17:08:58 2021 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Thu Mar 3 08:12:52 2022 +0100

    tdf#141550 tdf#141557 tdf#140975 sw: fix textbox crash
    
    and regressions by refactoring SwTextBoxHelper.
    
    tdf#141550 was a DOCX import regression from
    commit ff321dd36554d25f0817903becf4598065e0a194
    (tdf#140828 sw textbox: fix AS_CHAR regression),
    resulting bad horizontal position of the frame of
    the textbox.
    
    tdf#141557 was a regression resulting crash
    by changing text box anchoring "To Paragraph".
    
    tdf#140975 was an implementation error, resulting
    broken textbox by changing box anchoring to
    "As Character".
    
    In the SwTextBoxHelper class there were many
    repeating code parts for textbox positioning.
    Now these parts have been replaced with
    the calls of several new functions:
    
    1) getShapeFormat() returns with the SwFrameFormat
       of the UNO XShape;
    
    2) setWrapThrough() only sets the surround
       of the textframe (used in the code frequently);
    
    3) changeAnchor() sets the anchor of the textframe
       according to the anchor of the shape;
    
    4) doTextFramePositioning() sets the position of the
       textbox according to the shape (used in the code
       frequently);
    
    5) isAnchorTypeDifferent() shows difference of the
       anchor types of the shape and the textframe. Also it
       handles the situation of anchoring "As Character";
    
    6) isTextBoxHasValidTextFrame() checks if the shape
       is a part of a textbox or not.
    
    Every function returns with true on success, otherwise
    gives information to the log. All of them can be called
    with ShapeFormat or UNO XShape.
    
    The goal of this to make the possibility to advance this
    class for further functions – rotation, grouped
    textboxes etc. – without code copying.
    
    Change-Id: I4441cab87931f161d0ace4983646d6d2a68a7321
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113463
    Tested-by: László Németh <nem...@numbertext.org>
    Reviewed-by: László Németh <nem...@numbertext.org>
    (cherry picked from commit cb965f2cf6d68c284ccbd8a9635abfdfcc0538ca)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130900
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sw/inc/textboxhelper.hxx b/sw/inc/textboxhelper.hxx
index 2043b1ffcfb3..aa1940b5dd60 100644
--- a/sw/inc/textboxhelper.hxx
+++ b/sw/inc/textboxhelper.hxx
@@ -79,9 +79,48 @@ public:
     /// Similar to syncProperty(), but used by the internal API (e.g. for UI 
purposes).
     static void syncFlyFrameAttr(SwFrameFormat& rShape, SfxItemSet const& 
rSet);
 
+    /// Returns the SwFrameFormat* of the given UNO-XShape if exists otherwise 
nullptr.
+    static SwFrameFormat* 
getShapeFormat(css::uno::Reference<css::drawing::XShape> xShape);
+
     /// Copy shape attributes to the text frame
     static void updateTextBoxMargin(SdrObject* pObj);
 
+    /// Sets the surround to through for the textframe of the given shape,
+    /// not to interfere with the layout. Returns true on success.
+    static bool setWrapThrough(SwFrameFormat* pShape);
+
+    /// Sets the surround to through for the textframe of the given shape,
+    /// not to interfere with the layout. Returns true on success.
+    static bool setWrapThrough(css::uno::Reference<css::drawing::XShape> 
xShape);
+
+    /// Sets the anchor of the associated textframe of the given shape, and
+    /// returns true on success.
+    static bool changeAnchor(SwFrameFormat* pShape);
+
+    /// Sets the anchor of the associated textframe of the given shape, and
+    /// returns true on success.
+    static bool changeAnchor(css::uno::Reference<css::drawing::XShape> xShape);
+
+    /// Does the positioning for the associated textframe of the shape, and
+    /// returns true on success.
+    static bool doTextBoxPositioning(SwFrameFormat* pShape);
+
+    /// Does the positioning for the associated textframe of the shape, and
+    /// returns true on success.
+    static bool doTextBoxPositioning(css::uno::Reference<css::drawing::XShape> 
xShape);
+
+    /// Returns true if the anchor different for the  given shape, and the
+    /// associated textframe of the given shape.
+    /// Note: In case of AS_CHAR anchor the anchor type must be different,
+    /// because if not, layout breaks, but this situation also handled by
+    /// this function, and returns true in that case too.
+    static std::optional<bool> isAnchorTypeDifferent(SwFrameFormat* pShape);
+
+    /// Returns true if the given shape has a valid textframe.
+    static bool isTextBoxShapeHasValidTextFrame(SwFrameFormat* pShape);
+    /// Returns true if the given shape has a valid textframe.
+    static bool 
isTextBoxShapeHasValidTextFrame(css::uno::Reference<css::drawing::XShape> 
xShape);
+
     /**
      * If we have an associated TextFrame, then return that.
      *
diff --git a/sw/qa/extras/ooxmlexport/data/tdf141550.docx 
b/sw/qa/extras/ooxmlexport/data/tdf141550.docx
new file mode 100644
index 000000000000..170ebebba689
Binary files /dev/null and b/sw/qa/extras/ooxmlexport/data/tdf141550.docx differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
index 0b3f2321c4c6..d71c466258c3 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
@@ -21,6 +21,8 @@
 #include <com/sun/star/text/XTextTablesSupplier.hpp>
 #include <comphelper/propertysequence.hxx>
 #include <editeng/escapementitem.hxx>
+#include <textboxhelper.hxx>
+#include <unoprnms.hxx>
 
 char const DATA_DIRECTORY[] = "/sw/qa/extras/ooxmlexport/data/";
 
@@ -180,6 +182,21 @@ DECLARE_OOXMLEXPORT_TEST(testTdf133473_shadowSize, 
"tdf133473.docx")
     CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(200000), nSize1);
 }
 
+DECLARE_OOXMLEXPORT_TEST(testTdf141550, "tdf141550.docx")
+{
+    uno::Reference<drawing::XShape> xShape(getShape(1));
+    uno::Reference<text::XTextFrame> xFrame = 
SwTextBoxHelper::getUnoTextFrame(xShape);
+
+    CPPUNIT_ASSERT(xShape);
+    CPPUNIT_ASSERT(xFrame);
+
+    const sal_uInt16 nShapeRelOri = getProperty<sal_uInt16>(xShape, 
UNO_NAME_HORI_ORIENT_RELATION);
+    const sal_uInt16 nFrameRelOri = getProperty<sal_uInt16>(xFrame, 
UNO_NAME_HORI_ORIENT_RELATION);
+
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("Textbox fallen apart!", nShapeRelOri, 
nFrameRelOri);
+    // Without the fix in place it fails with difference.
+}
+
 DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testCommentDone, "CommentDone.docx")
 {
     xmlDocUniquePtr pXmlComm = parseExport("word/comments.xml");
diff --git a/sw/qa/extras/uiwriter/data3/tdf140975.docx 
b/sw/qa/extras/uiwriter/data3/tdf140975.docx
new file mode 100644
index 000000000000..d11a912d34a6
Binary files /dev/null and b/sw/qa/extras/uiwriter/data3/tdf140975.docx differ
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx 
b/sw/qa/extras/uiwriter/uiwriter3.cxx
index eedba24768a7..8ecfea76adbf 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -778,6 +778,36 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, TestAsCharTextBox)
     // Without the fix in place the two texboxes has been fallen apart, and  
asserts will broken.
 }
 
+CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf140975)
+{
+    // Load the bugdoc
+    load(DATA_DIRECTORY, "tdf140975.docx");
+    SwXTextDocument* pTextDoc = 
dynamic_cast<SwXTextDocument*>(mxComponent.get());
+    CPPUNIT_ASSERT(pTextDoc);
+
+    // Set the Anchor of the shape to As_Char
+    dispatchCommand(mxComponent, ".uno:JumpToNextFrame", {});
+    Scheduler::ProcessEventsToIdle();
+    dispatchCommand(mxComponent, ".uno:SetAnchorToChar", {});
+    Scheduler::ProcessEventsToIdle();
+
+    // Get the layout of the textbox
+    auto pExportDump = parseLayoutDump();
+    CPPUNIT_ASSERT(pExportDump);
+
+    const sal_Int32 nShpTop
+        = getXPath(pExportDump, 
"/root/page/body/txt[4]/anchored/SwAnchoredDrawObject/bounds",
+                   "top")
+              .toInt32();
+    const sal_Int32 nFrmTop
+        = getXPath(pExportDump, 
"/root/page/body/txt[4]/anchored/fly/infos/bounds", "top")
+              .toInt32();
+
+    // Without the fix in place, the frame has less value for Top than
+    // the shape. This means the frame is outside from the shape.
+    CPPUNIT_ASSERT_GREATER(nShpTop, nFrmTop);
+}
+
 CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf76636)
 {
     load(DATA_DIRECTORY, "tdf76636.doc");
diff --git a/sw/qa/uitest/data/tdf141557.docx b/sw/qa/uitest/data/tdf141557.docx
new file mode 100644
index 000000000000..9b0c6849bd44
Binary files /dev/null and b/sw/qa/uitest/data/tdf141557.docx differ
diff --git a/sw/source/core/doc/textboxhelper.cxx 
b/sw/source/core/doc/textboxhelper.cxx
index 6b9f87655d8d..186db15c75c2 100644
--- a/sw/source/core/doc/textboxhelper.cxx
+++ b/sw/source/core/doc/textboxhelper.cxx
@@ -616,7 +616,10 @@ void SwTextBoxHelper::syncProperty(SwFrameFormat* pShape, 
sal_uInt16 nWID, sal_u
                     aPropertyName = UNO_NAME_HORI_ORIENT;
                     break;
                 case MID_HORIORIENT_RELATION:
-                    aPropertyName = UNO_NAME_HORI_ORIENT_RELATION;
+                    if (pShape->GetAnchor().GetAnchorId() != 
RndStdIds::FLY_AS_CHAR)
+                        aPropertyName = UNO_NAME_HORI_ORIENT_RELATION;
+                    else
+                        return;
                     break;
                 case MID_HORIORIENT_POSITION:
                     aPropertyName = UNO_NAME_HORI_ORIENT_POSITION;
@@ -644,7 +647,10 @@ void SwTextBoxHelper::syncProperty(SwFrameFormat* pShape, 
sal_uInt16 nWID, sal_u
                     aPropertyName = UNO_NAME_VERT_ORIENT;
                     break;
                 case MID_VERTORIENT_RELATION:
-                    aPropertyName = UNO_NAME_VERT_ORIENT_RELATION;
+                    if (pShape->GetAnchor().GetAnchorId() != 
RndStdIds::FLY_AS_CHAR)
+                        aPropertyName = UNO_NAME_VERT_ORIENT_RELATION;
+                    else
+                        return;
                     break;
                 case MID_VERTORIENT_POSITION:
                     aPropertyName = UNO_NAME_VERT_ORIENT_POSITION;
@@ -675,110 +681,10 @@ void SwTextBoxHelper::syncProperty(SwFrameFormat* 
pShape, sal_uInt16 nWID, sal_u
             {
                 case MID_ANCHOR_ANCHORTYPE:
                 {
-                    uno::Reference<beans::XPropertySet> const xPropertySet(
-                        SwXTextFrame::CreateXTextFrame(*pFormat->GetDoc(), 
pFormat),
-                        uno::UNO_QUERY);
-                    // Surround (Wrap) has to be THROUGH always:
-                    xPropertySet->setPropertyValue(UNO_NAME_SURROUND,
-                                                   
uno::makeAny(text::WrapTextMode_THROUGH));
-                    // Use At_Char anchor instead of As_Char anchoring:
-                    if (aValue.get<text::TextContentAnchorType>()
-                        == 
text::TextContentAnchorType::TextContentAnchorType_AS_CHARACTER)
-                    {
-                        if (const auto aPos = 
pShape->GetAnchor().GetContentAnchor())
-                        {
-                            xPropertySet->setPropertyValue(
-                                UNO_NAME_ANCHOR_TYPE,
-                                uno::makeAny(text::TextContentAnchorType::
-                                                 
TextContentAnchorType_AT_CHARACTER));
-                            xPropertySet->setPropertyValue(
-                                UNO_NAME_HORI_ORIENT_RELATION,
-                                uno::makeAny(text::RelOrientation::CHAR));
-
-                            auto pAnch = pFormat->GetAnchor();
-                            
pAnch.SetAnchor(pShape->GetAnchor().GetContentAnchor());
-                            tools::Rectangle aRect(getTextRectangle(pShape, 
false));
-
-                            SwFormatHoriOrient 
aNewHOri(pFormat->GetHoriOrient());
-                            aNewHOri.SetPos(aRect.getX());
-
-                            SwFormatVertOrient 
aNewVOri(pFormat->GetVertOrient());
-                            aNewVOri.SetPos(aRect.getY());
-
-                            pFormat->SetFormatAttr(pAnch);
-                            // tdf#140598: Do not apply wrong rectangle 
position.
-                            if (aRect.TopLeft() != Point(0, 0))
-                            {
-                                pFormat->SetFormatAttr(aNewHOri);
-                                pFormat->SetFormatAttr(aNewVOri);
-                            }
-                            else
-                                SAL_WARN("sw.core",
-                                         "SwTextBoxHelper::syncProperty: 
Repositioning failed!");
-                        }
-
-                        return;
-                    }
-                    else // Otherwise copy the anchor type of the shape
-                    {
-                        // tdf#140828: Do not keep CHAR rel-orientation:
-                        
xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
-                                                       
uno::Any(text::RelOrientation::FRAME));
-                        xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_TYPE, 
aValue);
-                    }
-                    // After anchoring the position must be set as well:
-                    if (aValue.get<text::TextContentAnchorType>()
-                        == 
text::TextContentAnchorType::TextContentAnchorType_AT_PAGE)
-                    {
-                        if (pShape->GetAnchor().GetPageNum())
-                            xPropertySet->setPropertyValue(
-                                UNO_NAME_ANCHOR_PAGE_NO,
-                                
uno::makeAny(pShape->GetAnchor().GetPageNum()));
-                        else
-                        {
-                            SAL_WARN("sw.core", 
"SwTextBoxHelper::syncProperty: Invalid Page Num!");
-                            return;
-                        }
-                    }
+                    setWrapThrough(pShape);
+                    changeAnchor(pShape);
+                    doTextBoxPositioning(pShape);
 
-                    // At-Content Anchors have to be synced:
-                    if (aValue.get<text::TextContentAnchorType>()
-                            == 
text::TextContentAnchorType::TextContentAnchorType_AT_PARAGRAPH
-                        || aValue.get<text::TextContentAnchorType>()
-                               == 
text::TextContentAnchorType::TextContentAnchorType_AT_CHARACTER)
-                    {
-                        // If the shape has content...
-                        if (auto aPos = pShape->GetAnchor().GetContentAnchor())
-                        {
-                            SwFormatAnchor aAnch(pFormat->GetAnchor());
-                            // ...set it for the textframe too.
-                            aAnch.SetAnchor(aPos);
-                            pFormat->SetFormatAttr(aAnch);
-                        }
-                        else
-                            SAL_WARN("sw.core",
-                                     "SwTextBoxHelper::syncProperty: Anchor 
without content!");
-                    }
-                    // And the repositioning:
-                    if (pShape->GetAnchor().GetAnchorId() != 
RndStdIds::FLY_AS_CHAR)
-                    {
-                        tools::Rectangle aRect(getTextRectangle(pShape, 
false));
-
-                        // tdf#140598: Do not apply wrong rectangle position.
-                        if (aRect.TopLeft() != Point(0, 0))
-                        {
-                            SwFormatHoriOrient 
aNewHOri(pShape->GetHoriOrient());
-                            aNewHOri.SetPos(aNewHOri.GetPos() + aRect.getX());
-                            SwFormatVertOrient 
aNewVOri(pShape->GetVertOrient());
-                            aNewVOri.SetPos(aNewVOri.GetPos() + aRect.getY());
-
-                            pFormat->SetFormatAttr(aNewHOri);
-                            pFormat->SetFormatAttr(aNewVOri);
-                        }
-                        else
-                            SAL_WARN("sw.core",
-                                     "SwTextBoxHelper::syncProperty: 
Repositioning failed!");
-                    }
                     return;
                 }
                 break;
@@ -1051,6 +957,16 @@ void SwTextBoxHelper::syncFlyFrameAttr(SwFrameFormat& 
rShape, SfxItemSet const&
         pFormat->GetDoc()->SetFlyFrameAttr(*pFormat, aTextBoxSet);
 }
 
+SwFrameFormat* SwTextBoxHelper::getShapeFormat(uno::Reference<drawing::XShape> 
xShape)
+{
+    if (xShape)
+        if (auto pShape = dynamic_cast<SwXShape*>(xShape.get()))
+            if (SwFrameFormat* pFormat = pShape->GetFrameFormat())
+                return pFormat;
+    SAL_WARN("sw.core", "SwTextBoxHelper::getShapeFormat: No Shape Format!");
+    return nullptr;
+}
+
 void SwTextBoxHelper::updateTextBoxMargin(SdrObject* pObj)
 {
     if (!pObj)
@@ -1085,4 +1001,240 @@ void SwTextBoxHelper::updateTextBoxMargin(SdrObject* 
pObj)
     //             
xPropertySet->getPropertyValue(UNO_NAME_TEXT_AUTOGROWHEIGHT));
 }
 
+bool SwTextBoxHelper::setWrapThrough(SwFrameFormat* pShape)
+{
+    OUString sErrMsg;
+    if (isTextBoxShapeHasValidTextFrame(pShape))
+    {
+        if (auto pFormat = getOtherTextBoxFormat(pShape, RES_DRAWFRMFMT))
+        {
+            if (auto xFrame = 
SwXTextFrame::CreateXTextFrame(*pFormat->GetDoc(), pFormat))
+                try
+                {
+                    uno::Reference<beans::XPropertySet> const 
xPropertySet(xFrame, uno::UNO_QUERY);
+                    xPropertySet->setPropertyValue(UNO_NAME_SURROUND,
+                                                   
uno::makeAny(text::WrapTextMode_THROUGH));
+                    return true;
+                }
+                catch (uno::Exception& e)
+                {
+                    sErrMsg = "Exception caught: " + e.Message;
+                }
+            else
+                sErrMsg = "No XTextFrame!";
+        }
+        else
+            sErrMsg = "No Other TextBox Format!";
+    }
+    else
+        sErrMsg = "Not a Valid TextBox object!";
+
+    SAL_WARN("sw.core", "SwTextBoxHelper::setWrapThrough: " << sErrMsg);
+    return false;
+}
+
+bool SwTextBoxHelper::setWrapThrough(uno::Reference<drawing::XShape> xShape)
+{
+    if (auto pShape = getShapeFormat(xShape))
+        return setWrapThrough(pShape);
+    return false;
+}
+
+bool SwTextBoxHelper::changeAnchor(SwFrameFormat* pShape)
+{
+    if (isTextBoxShapeHasValidTextFrame(pShape))
+    {
+        if (auto pFormat = getOtherTextBoxFormat(pShape, RES_DRAWFRMFMT))
+        {
+            const SwFormatAnchor& rOldAnch = pFormat->GetAnchor();
+            const SwFormatAnchor& rNewAnch = pShape->GetAnchor();
+
+            const auto pOldCnt = rOldAnch.GetContentAnchor();
+            const auto pNewCnt = rNewAnch.GetContentAnchor();
+
+            const uno::Any aShapeHorRelOrient
+                = uno::makeAny(pShape->GetHoriOrient().GetRelationOrient());
+
+            if (isAnchorTypeDifferent(pShape))
+            {
+                try
+                {
+                    uno::Reference<beans::XPropertySet> const xPropertySet(
+                        SwXTextFrame::CreateXTextFrame(*pFormat->GetDoc(), 
pFormat),
+                        uno::UNO_QUERY);
+                    if (pOldCnt && rNewAnch.GetAnchorId() == 
RndStdIds::FLY_AT_PAGE
+                        && rNewAnch.GetPageNum())
+                    {
+                        uno::Any aValue(text::TextContentAnchorType_AT_PAGE);
+                        
xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
+                                                       aShapeHorRelOrient);
+                        xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_TYPE, 
aValue);
+                        xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_PAGE_NO,
+                                                       
uno::Any(rNewAnch.GetPageNum()));
+                    }
+                    else if (rOldAnch.GetAnchorId() == RndStdIds::FLY_AT_PAGE 
&& pNewCnt)
+                    {
+                        if (rNewAnch.GetAnchorId() == RndStdIds::FLY_AS_CHAR)
+                        {
+                            uno::Any 
aValue(text::TextContentAnchorType_AT_CHARACTER);
+                            
xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_TYPE, aValue);
+                            
xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
+                                                           
uno::Any(text::RelOrientation::CHAR));
+                            xPropertySet->setPropertyValue(
+                                UNO_NAME_VERT_ORIENT_RELATION,
+                                uno::Any(text::RelOrientation::PRINT_AREA));
+                            SwFormatAnchor aPos(pFormat->GetAnchor());
+                            aPos.SetAnchor(pNewCnt);
+                            pFormat->SetFormatAttr(aPos);
+                        }
+                        else
+                        {
+                            uno::Any 
aValue(mapAnchorType(rNewAnch.GetAnchorId()));
+                            
xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
+                                                           aShapeHorRelOrient);
+                            
xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_TYPE, aValue);
+                            pFormat->SetFormatAttr(rNewAnch);
+                        }
+                    }
+                    else
+                    {
+                        if (rNewAnch.GetAnchorId() == RndStdIds::FLY_AS_CHAR)
+                        {
+                            uno::Any 
aValue(text::TextContentAnchorType_AT_CHARACTER);
+                            
xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_TYPE, aValue);
+                            
xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
+                                                           
uno::Any(text::RelOrientation::CHAR));
+                            xPropertySet->setPropertyValue(
+                                UNO_NAME_VERT_ORIENT_RELATION,
+                                uno::Any(text::RelOrientation::PRINT_AREA));
+                            SwFormatAnchor aPos(pFormat->GetAnchor());
+                            aPos.SetAnchor(pNewCnt);
+                            pFormat->SetFormatAttr(aPos);
+                        }
+                        else
+                        {
+                            
xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
+                                                           aShapeHorRelOrient);
+                            pFormat->SetFormatAttr(pShape->GetAnchor());
+                        }
+                    }
+                }
+                catch (uno::Exception& e)
+                {
+                    SAL_WARN("sw.core", "SwTextBoxHelper::changeAnchor(): " << 
e.Message);
+                }
+            }
+
+            return doTextBoxPositioning(pShape);
+        }
+    }
+    return false;
+}
+
+bool SwTextBoxHelper::changeAnchor(uno::Reference<drawing::XShape> xShape)
+{
+    if (auto pShape = getShapeFormat(xShape))
+        return changeAnchor(pShape);
+    return false;
+}
+
+bool SwTextBoxHelper::doTextBoxPositioning(SwFrameFormat* pShape)
+{
+    if (isTextBoxShapeHasValidTextFrame(pShape))
+    {
+        if (auto pFormat = getOtherTextBoxFormat(pShape, RES_DRAWFRMFMT))
+        {
+            if (pShape->GetAnchor().GetAnchorId() == RndStdIds::FLY_AS_CHAR)
+            {
+                tools::Rectangle aRect(getTextRectangle(pShape, false));
+
+                SwFormatHoriOrient aNewHOri(pFormat->GetHoriOrient());
+                aNewHOri.SetPos(aRect.getX());
+
+                SwFormatVertOrient aNewVOri(pFormat->GetVertOrient());
+                aNewVOri.SetPos(aRect.getY() + 
pShape->GetVertOrient().GetPos());
+
+                // tdf#140598: Do not apply wrong rectangle position.
+                if (aRect.TopLeft() != Point(0, 0))
+                {
+                    pFormat->SetFormatAttr(aNewHOri);
+                    pFormat->SetFormatAttr(aNewVOri);
+                }
+                else
+                    SAL_WARN("sw.core", "SwTextBoxHelper::syncProperty: 
Repositioning failed!");
+            }
+            else
+            {
+                tools::Rectangle aRect(getTextRectangle(pShape, false));
+
+                // tdf#140598: Do not apply wrong rectangle position.
+                if (aRect.TopLeft() != Point(0, 0))
+                {
+                    SwFormatHoriOrient aNewHOri(pShape->GetHoriOrient());
+                    aNewHOri.SetPos(aNewHOri.GetPos() + aRect.getX());
+                    SwFormatVertOrient aNewVOri(pShape->GetVertOrient());
+                    aNewVOri.SetPos(aNewVOri.GetPos() + aRect.getY());
+
+                    pFormat->SetFormatAttr(aNewHOri);
+                    pFormat->SetFormatAttr(aNewVOri);
+                }
+                else
+                    SAL_WARN("sw.core", "SwTextBoxHelper::syncProperty: 
Repositioning failed!");
+            }
+            return true;
+        }
+    }
+    return false;
+}
+
+bool SwTextBoxHelper::doTextBoxPositioning(uno::Reference<drawing::XShape> 
xShape)
+{
+    if (auto pShape = getShapeFormat(xShape))
+        return doTextBoxPositioning(pShape);
+    return false;
+}
+
+std::optional<bool> SwTextBoxHelper::isAnchorTypeDifferent(SwFrameFormat* 
pShape)
+{
+    std::optional<bool> bRet;
+    if (isTextBoxShapeHasValidTextFrame(pShape))
+    {
+        if (auto pFormat = getOtherTextBoxFormat(pShape, RES_DRAWFRMFMT))
+        {
+            if (pShape->GetAnchor().GetAnchorId() == RndStdIds::FLY_AS_CHAR)
+                bRet = (pFormat->GetAnchor().GetAnchorId() != 
RndStdIds::FLY_AT_CHAR
+                        && pFormat->GetAnchor().GetAnchorId() != 
RndStdIds::FLY_AS_CHAR);
+            else
+                bRet = pFormat->GetAnchor().GetAnchorId() != 
pShape->GetAnchor().GetAnchorId();
+        }
+    }
+    return bRet;
+}
+
+bool SwTextBoxHelper::isTextBoxShapeHasValidTextFrame(SwFrameFormat* pShape)
+{
+    OUString sErrMsg;
+    if (pShape && pShape->Which() == RES_DRAWFRMFMT)
+        if (auto pFormat = getOtherTextBoxFormat(pShape, RES_DRAWFRMFMT))
+            if (pFormat && pFormat->Which() == RES_FLYFRMFMT)
+                return true;
+            else
+                sErrMsg = "Shape do not have valid textframe!";
+        else
+            sErrMsg = "Shape do not have associated frame!";
+    else
+        sErrMsg = "Not valid shape!";
+
+    SAL_WARN("sw.core", "SwTextBoxHelper::isTextBoxShapeHasValidTextFrame: " 
<< sErrMsg);
+    return false;
+}
+
+bool 
SwTextBoxHelper::isTextBoxShapeHasValidTextFrame(uno::Reference<drawing::XShape>
 xShape)
+{
+    if (auto pShape = getShapeFormat(xShape))
+        return isTextBoxShapeHasValidTextFrame(pShape);
+
+    return false;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/text/porfly.cxx b/sw/source/core/text/porfly.cxx
index 9426359156f1..96bdebd58adc 100644
--- a/sw/source/core/text/porfly.cxx
+++ b/sw/source/core/text/porfly.cxx
@@ -363,6 +363,7 @@ void SwFlyCntPortion::SetBase( const SwTextFrame& rFrame, 
const Point &rBase,
             if (!aPos->nNode.GetNode().FindFooterStartNode())
             {
                 aVert.SetVertOrient(css::text::VertOrientation::NONE);
+                aVert.SetRelationOrient(css::text::RelOrientation::FRAME);
                 sal_Int32 const nTop = aTextRectangle.getY() - 
rFrame.getFrameArea().Top()
                                        - rFrame.getFramePrintArea().Top();
                 aVert.SetPos(nTop);

Reply via email to