tdf#129888 DOCX shape import: handle o:allowincell

(VML) and layoutInCell (DrawingML) attributes to fix
regressions caused by commit 10f29d8bf05d44ca8bc11d34d1294ec17f8ac0f1
(tdf#87569 tdf#109411 DOCX import: fix shape anchor in tables).

Position of shapes anchored to tables is calculated from
the cell margin only if the previous attributes allow that.

Change-Id: Ifcfcb7f4959aea522dd45dff00cefd1bb9f4edda
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86922
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
Signed-off-by: xisco <xiscofauli@libreoffice.org>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86980
Reviewed-by: Attila Bakos <bakos.attilakaroly@nisz.hu>
diff --git a/sw/qa/extras/ooxmlexport/data/tdf129888dml.docx b/sw/qa/extras/ooxmlexport/data/tdf129888dml.docx
new file mode 100644
index 0000000..1f6b030
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf129888dml.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/data/tdf129888vml.docx b/sw/qa/extras/ooxmlexport/data/tdf129888vml.docx
new file mode 100644
index 0000000..ee7c60f
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf129888vml.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
index 516d65d..bb9e393 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
@@ -42,6 +42,34 @@ DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf128207, "tdf128207.docx")
    assertXPathContent(p_XmlDoc, "/w:document/w:body/w:p[1]/w:r[1]/w:drawing/wp:anchor/wp:positionH/wp:posOffset", "4445");
}

DECLARE_OOXMLIMPORT_TEST(testTdf129888vml, "tdf129888vml.docx")
{
    //the line shape has anchor in the first cell however it has to
    //be positioned to an another cell. To reach this we must handle
    //the o:allowincell attribute of the shape, and its position has
    //to be calculated from the page frame instead of the table:

    uno::Reference<beans::XPropertySet> xShapeProperties(getShape(1), uno::UNO_QUERY);
    sal_Int16 nValue;
    xShapeProperties->getPropertyValue("HoriOrientRelation") >>= nValue;
    CPPUNIT_ASSERT_EQUAL_MESSAGE("tdf129888vml The line shape has bad place!",
                                 text::RelOrientation::PAGE_FRAME, nValue);
}

DECLARE_OOXMLIMPORT_TEST(testTdf129888dml, "tdf129888dml.docx")
{
    //the shape has anchor in the first cell however it has to
    //be positioned to the right side of the page. To reach this we must handle
    //the layoutInCell attribute of the shape, and its position has
    //to be calculated from the page frame instead of the table:

    uno::Reference<beans::XPropertySet> xShapeProperties(getShape(1), uno::UNO_QUERY);
    sal_Int16 nValue;
    xShapeProperties->getPropertyValue("HoriOrientRelation") >>= nValue;
    CPPUNIT_ASSERT_EQUAL_MESSAGE("tdf129888dml The shape has bad place!",
                                 text::RelOrientation::PAGE_FRAME, nValue);
}

DECLARE_OOXMLEXPORT_TEST(testTdf87569v, "tdf87569_vml.docx")
{
    //the original tdf87569 sample has vml shapes...
diff --git a/writerfilter/source/dmapper/DomainMapper.cxx b/writerfilter/source/dmapper/DomainMapper.cxx
index 3fd2e98..f7f2454 100644
--- a/writerfilter/source/dmapper/DomainMapper.cxx
+++ b/writerfilter/source/dmapper/DomainMapper.cxx
@@ -3046,21 +3046,6 @@ void DomainMapper::lcl_startShape(uno::Reference<drawing::XShape> const& xShape)
        m_pImpl->PushPendingShape(xShape);

    m_pImpl->SetIsFirstParagraphInShape(true);

    //tdf#87569: Fix table layout with correcting anchoring
    //If anchored object is in table, Word calculates its position from cell border
    //instead of page (what is set in the sample document)
    if (m_pImpl->m_nTableDepth > 0) //if we had a table
    {
        uno::Reference<beans::XPropertySet> xShapePropSet(xShape, uno::UNO_QUERY);
        sal_Int16 nCurrentHorOriRel; //A temp variable for storaging the current setting
        xShapePropSet->getPropertyValue("HoriOrientRelation") >>= nCurrentHorOriRel;
        //and the correction:
        if (nCurrentHorOriRel == text::RelOrientation::PAGE_FRAME)
            xShapePropSet->setPropertyValue("HoriOrientRelation",
                                            uno::makeAny(text::RelOrientation::FRAME));
    }

}

void DomainMapper::lcl_endShape( )
diff --git a/writerfilter/source/dmapper/GraphicImport.cxx b/writerfilter/source/dmapper/GraphicImport.cxx
index df4ae45..409bdb1 100644
--- a/writerfilter/source/dmapper/GraphicImport.cxx
+++ b/writerfilter/source/dmapper/GraphicImport.cxx
@@ -868,7 +868,7 @@ void GraphicImport::lcl_attribute(Id nName, Value& rValue)

                        //tdf#109411 If anchored object is in table, Word calculates its position from cell border
                        //instead of page (what is set in the sample document)
                        if (m_pImpl->rDomainMapper.IsInTable() &&
                        if (m_pImpl->rDomainMapper.IsInTable() && m_pImpl->bLayoutInCell &&
                            m_pImpl->nHoriRelation == text::RelOrientation::PAGE_FRAME && IsGraphic())
                        {
                            m_pImpl->nHoriRelation = text::RelOrientation::FRAME;
diff --git a/writerfilter/source/ooxml/OOXMLFastContextHandler.cxx b/writerfilter/source/ooxml/OOXMLFastContextHandler.cxx
index 6f7839d0..19dc3bb 100644
--- a/writerfilter/source/ooxml/OOXMLFastContextHandler.cxx
+++ b/writerfilter/source/ooxml/OOXMLFastContextHandler.cxx
@@ -17,6 +17,8 @@
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/text/RelOrientation.hpp>
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/xml/sax/FastShapeContextHandler.hpp>
@@ -65,6 +67,7 @@ OOXMLFastContextHandler::OOXMLFastContextHandler
  mpStream(nullptr),
  mnTableDepth(0),
  inPositionV(false),
  mbLayoutInCell(true),
  m_xContext(context),
  m_bDiscardChildren(false),
  m_bTookChoice(false)
@@ -85,6 +88,7 @@ OOXMLFastContextHandler::OOXMLFastContextHandler(OOXMLFastContextHandler * pCont
  mpParserState(pContext->mpParserState),
  mnTableDepth(pContext->mnTableDepth),
  inPositionV(pContext->inPositionV),
  mbLayoutInCell(pContext->mbLayoutInCell),
  m_xContext(pContext->m_xContext),
  m_bDiscardChildren(pContext->m_bDiscardChildren),
  m_bTookChoice(pContext->m_bTookChoice)
@@ -1663,6 +1667,21 @@ void OOXMLFastContextHandlerShape::sendShape( Token_t Element )

            bool bIsPicture = Element == ( NMSP_dmlPicture | XML_pic );


            //tdf#87569: Fix table layout with correcting anchoring
            //If anchored object is in table, Word calculates its position from cell border
            //instead of page (what is set in the sample document)
            if (mnTableDepth > 0 && mbLayoutInCell) //if we had a table
            {
                uno::Reference<beans::XPropertySet> xShapePropSet(xShape, uno::UNO_QUERY);
                sal_Int16 nCurrentHorOriRel; //A temp variable for storaging the current setting
                xShapePropSet->getPropertyValue("HoriOrientRelation") >>= nCurrentHorOriRel;
                //and the correction:
                if (nCurrentHorOriRel == com::sun::star::text::RelOrientation::PAGE_FRAME)
                    xShapePropSet->setPropertyValue("HoriOrientRelation",
                                                    uno::makeAny(text::RelOrientation::FRAME));
            }

            // Notify the dmapper that the shape is ready to use
            if ( !bIsPicture )
            {
@@ -1735,6 +1754,11 @@ OOXMLFastContextHandlerShape::lcl_createFastChildContext
                                                           pChildContext,
                                                           this);

                    //tdf129888 store allowincell attribute of the VML shape
                    if (Attribs->hasAttribute(NMSP_vmlOffice | XML_allowincell))
                        mbLayoutInCell
                            = !(Attribs->getValue(NMSP_vmlOffice | XML_allowincell) == "f");

                    if (!bGroupShape)
                    {
                        pWrapper->addNamespace(NMSP_doc);
diff --git a/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx b/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx
index d5de5a7..0cc3fb1 100644
--- a/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx
+++ b/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx
@@ -226,6 +226,7 @@ protected:
    const css::uno::Reference< css::uno::XComponentContext >& getComponentContext() const { return m_xContext;}

    bool inPositionV;
    bool mbLayoutInCell; // o:allowincell
    OOXMLValue::Pointer_t mpGridAfter;

private: