sw/qa/extras/odfimport/data/forcepoint101.fodt  |   13 +++++++++++++
 sw/qa/extras/odfimport/odfimport.cxx            |    6 ++++++
 sw/source/core/unocore/unodraw.cxx              |    4 +++-
 sw/source/core/unocore/unoframe.cxx             |    1 +
 xmloff/source/text/XMLTextShapeImportHelper.cxx |   15 ++++++++++-----
 5 files changed, 33 insertions(+), 6 deletions(-)

New commits:
commit 5188a1aa3c6ca44e6bdf0991546109fcd3ab3400
Author:     Michael Stahl <michael.st...@allotropia.de>
AuthorDate: Mon Apr 4 17:19:51 2022 +0200
Commit:     Caolán McNamara <caol...@redhat.com>
CommitDate: Tue Apr 5 10:44:41 2022 +0200

    forcepoint#101 sw: fix inserting at-page shapes to not set anchor pos
    
    The problem is that for an at-page anchor SwXDrawPage::add() sets an
    anchor position, but then a fieldmark is inserted and the node with the
    anchor position is deleted.
    
    The code that sets the surprising anchor position for at-page shape was
    added in commit cf345e026fc0378c4436173c8064717c7db4d538 "Fix #91289#:
    Draw objects anchored at page but without page number" and is similar to
    the code for fly-frames added in commit
    2e6d999cf8ceacabb94b11feee6ee0808c2a75bc "If no page number is given for
    page bound frames, calculate it using cursor" except that it is missing
    a check for GetPageNum() == 0, for no obvious reason.
    
    The matching code to replace the anchor position with a page number is
    in SwFlyFrameFormat::MakeFrames() and was added in commit
    572a48cd16f6ce3a68ffead44a975698954dfe11 "Fix #85019#: Insertion of
    flyframes without correct pagenumber"; it's not clear if this is only
    needed for shapes inserted via the above 2 UNO APIs or also for shapes
    inserted via UI.
    
    Then there is the issue that the page number is set in
    XMLTextShapeImportHelper::addShape() only after the shape is inserted,
    which was added and commented in commit
    b5fc47fe5c2892dd712e2bda922656eacbd932f1, earlier than all the above
    commits; testing shows that if the page number is set before inserting
    the shape, the shape ends up on the expected page anyway.
    
    Let's change this:
    1. the page number is set before inserting the shape
    2. at-page anchor with page number is left as is
    3. at-page anchor without page number gets anchor position and is changed
       to at-char
    
    Change-Id: I2f874bec8424de5eee94cf022e3b49382eaa55f0
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132479
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.st...@allotropia.de>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132530
    Tested-by: Caolán McNamara <caol...@redhat.com>
    Reviewed-by: Caolán McNamara <caol...@redhat.com>

diff --git a/sw/qa/extras/odfimport/data/forcepoint101.fodt 
b/sw/qa/extras/odfimport/data/forcepoint101.fodt
new file mode 100644
index 000000000000..7210888262dc
--- /dev/null
+++ b/sw/qa/extras/odfimport/data/forcepoint101.fodt
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<office:document xmlns:officeooo="http://openoffice.org/2009/office"; 
xmlns:css3t="http://www.w3.org/TR/css3-text/"; 
xmlns:grddl="http://www.w3.org/2003/g/data-view#"; 
xmlns:xhtml="http://www.w3.org/1999/xhtml"; 
xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"; 
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
xmlns:oooc="http://openoffice.org/2004/calc"; 
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
xmlns:ooow="http://openoffice.org/2004/writer"; 
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
xmlns:dc="http://purl.org/dc/elements/1.1/"; 
xmlns:rpt="http://openoffice.org/2005/report"; 
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
xmlns:config="urn:oasis:names:tc:opendocument:xmlns
 :config:1.0" xmlns:xlink="http://www.w3.org/1999/xlink"; 
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
xmlns:ooo="http://openoffice.org/2004/office"; 
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0"
 xmlns:tableooo="http://openoffice.org/2009/table"; 
xmlns:drawooo="http://openoffice.org/2010/draw"; 
xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"
 xmlns:dom="http://www.w3.org/2001/xml-events"; 
xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" 
xmlns:math="http://www.w3.org/1998/Math/MathML"; 
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="ur
 n:oasis:names:tc:opendocument:xmlns:script:1.0" 
xmlns:xforms="http://www.w3.org/2002/xforms"; office:version="1.3" 
office:mimetype="application/vnd.oasis.opendocument.text">
+ <office:body>
+  <office:text>
+   <draw:custom-shape text:anchor-type="page" texnumber="1" draw:z-index="1" 
draw:name="Shape2" svg:width="2.276cm" svg:height="2.276cm" svg:x="4.752cm" 
svg:y="2.902cm">
+    <text:p/>
+   </draw:custom-shape>
+   <text:p>
+   <draw:custom-shape draw:z-index="0" draw:name="Shape1" svg:width="3.44cm" 
svg:height="2.065cm" svg:x="-1.337cm" 
svg:y="0.912cm"><text:p/></draw:custom-shape>
+   <field:fieldmark-start text:name="__Fieldmark__0_1348794289" 
field:type="vnd.oasis.opendocument.field.FORMTEXT"/>foobar<field:fieldmark-end/></text:p>
+  </office:text>
+ </office:body>
+</office:document>
diff --git a/sw/qa/extras/odfimport/odfimport.cxx 
b/sw/qa/extras/odfimport/odfimport.cxx
index 336cca824232..9c177f981aa4 100644
--- a/sw/qa/extras/odfimport/odfimport.cxx
+++ b/sw/qa/extras/odfimport/odfimport.cxx
@@ -1334,5 +1334,11 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf146597)
     // This was creashed before.
 }
 
+// just care that it doesn't crash/assert
+CPPUNIT_TEST_FIXTURE(Test, testForcepoint101)
+{
+    load(mpTestDocumentPath, "forcepoint101.fodt");
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/unocore/unodraw.cxx 
b/sw/source/core/unocore/unodraw.cxx
index e61a66850001..5c5811ee3934 100644
--- a/sw/source/core/unocore/unodraw.cxx
+++ b/sw/source/core/unocore/unodraw.cxx
@@ -668,9 +668,11 @@ void SwXDrawPage::add(const uno::Reference< 
drawing::XShape > & xShape)
         {
                     aAnchor.SetType(RndStdIds::FLY_AS_CHAR);
         }
-        else if (RndStdIds::FLY_AT_PAGE == aAnchor.GetAnchorId())
+        else if (RndStdIds::FLY_AT_PAGE == aAnchor.GetAnchorId()
+                    && 0 == aAnchor.GetPageNum())
         {
             aAnchor.SetAnchor(pInternalPam->Start());
+            aAnchor.SetType(RndStdIds::FLY_AT_CHAR); // convert invalid at-page
         }
 
     }
diff --git a/sw/source/core/unocore/unoframe.cxx 
b/sw/source/core/unocore/unoframe.cxx
index 4c35a9248665..35f78c4fca1b 100644
--- a/sw/source/core/unocore/unoframe.cxx
+++ b/sw/source/core/unocore/unoframe.cxx
@@ -2764,6 +2764,7 @@ void 
SwXFrame::attachToRange(uno::Reference<text::XTextRange> const& xTextRange,
                  0 == static_cast<const SwFormatAnchor*>(pItem)->GetPageNum() )
         {
             SwFormatAnchor aAnchor( *static_cast<const SwFormatAnchor*>(pItem) 
);
+            aAnchor.SetType(RndStdIds::FLY_AT_CHAR); // convert invalid at-page
             aAnchor.SetAnchor( aPam.GetPoint() );
             aFrameSet.Put(aAnchor);
         }
diff --git a/xmloff/source/text/XMLTextShapeImportHelper.cxx 
b/xmloff/source/text/XMLTextShapeImportHelper.cxx
index 89b7de528c95..f4a62f4ad9b7 100644
--- a/xmloff/source/text/XMLTextShapeImportHelper.cxx
+++ b/xmloff/source/text/XMLTextShapeImportHelper.cxx
@@ -119,11 +119,7 @@ void XMLTextShapeImportHelper::addShape(
     // anchor type
     xPropSet->setPropertyValue( gsAnchorType, Any(eAnchorType) );
 
-    Reference < XTextContent > xTxtCntnt( rShape, UNO_QUERY );
-    xTxtImport->InsertTextContent( xTxtCntnt );
-
-    // page number (must be set after the frame is inserted, because it
-    // will be overwritten then inserting the frame.
+    // page number must be set before the frame is inserted
     switch( eAnchorType )
     {
     case TextContentAnchorType_AT_PAGE:
@@ -133,6 +129,15 @@ void XMLTextShapeImportHelper::addShape(
             xPropSet->setPropertyValue( gsAnchorPageNo, Any(nPage) );
         }
         break;
+    default:
+        break;
+    }
+
+    Reference < XTextContent > xTxtCntnt( rShape, UNO_QUERY );
+    xTxtImport->InsertTextContent( xTxtCntnt );
+
+    switch( eAnchorType )
+    {
     case TextContentAnchorType_AS_CHARACTER:
         xPropSet->setPropertyValue( gsVertOrientPosition, Any(nY) );
         break;

Reply via email to