tdf#99631 DOCX: keep zoom of embedded XLSX

and other OLE objects by handling their VisibleArea settings.

(Cherry-picked from the following commits:
5c1a6c9adb5ccfbb869a0a7ac730d8860a1bf405
"DOCX import: set 1:1 scale in embedded XLSX"
69ce3374f90847f357249a41118314508da6bb98
"DOCX import: keep zoom of embedded XLSX"
fc50869a818f5411bf2bbd34db398078308f2ba3
"DOCX export: keep zoom of embedded XLSX")

Change-Id: I3f246b779afd145fe260af83173c1944df21fb1a
Reviewed-on: https://gerrit.libreoffice.org/65271
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
diff --git a/sw/inc/cmdid.h b/sw/inc/cmdid.h
index d829c3d..28a13ea 100644
--- a/sw/inc/cmdid.h
+++ b/sw/inc/cmdid.h
@@ -599,6 +599,8 @@
#define FN_UNO_HIDDEN                       (FN_EXTRA2 + 122)
#define FN_UNO_STYLE_INTEROP_GRAB_BAG       (FN_EXTRA2 + 123)
#define FN_UNO_TABLE_TEMPLATE_NAME          (FN_EXTRA2 + 124)
#define FN_UNO_VISIBLE_AREA_WIDTH           (FN_EXTRA2 + 125)
#define FN_UNO_VISIBLE_AREA_HEIGHT          (FN_EXTRA2 + 126)

// Area: Help
// Region: Traveling & Selection
diff --git a/sw/inc/unoframe.hxx b/sw/inc/unoframe.hxx
index 41dc329..43876e9 100644
--- a/sw/inc/unoframe.hxx
+++ b/sw/inc/unoframe.hxx
@@ -72,6 +72,8 @@ private:
    std::unique_ptr<SwPaM>          m_pCopySource;

    sal_Int64                       m_nDrawAspect;
    sal_Int64                       m_nVisibleAreaWidth;
    sal_Int64                       m_nVisibleAreaHeight;

protected:
    css::uno::Reference< css::beans::XPropertySet > mxStyleData;
diff --git a/sw/inc/unoprnms.hxx b/sw/inc/unoprnms.hxx
index a168bbc..30731e1 100644
--- a/sw/inc/unoprnms.hxx
+++ b/sw/inc/unoprnms.hxx
@@ -821,6 +821,8 @@
#define UNO_NAME_RSID "Rsid"
#define UNO_NAME_PARRSID "ParRsid"
#define UNO_NAME_DRAW_ASPECT "DrawAspect"
#define UNO_NAME_VISIBLE_AREA_WIDTH "VisibleAreaWidth"
#define UNO_NAME_VISIBLE_AREA_HEIGHT "VisibleAreaHeight"

#define UNO_NAME_LINE_STYLE "LineStyle"
#define UNO_NAME_LINE_WIDTH "LineWidth"
diff --git a/sw/qa/extras/odfexport/data/tdf99631.docx b/sw/qa/extras/odfexport/data/tdf99631.docx
new file mode 100644
index 0000000..c72950b
--- /dev/null
+++ b/sw/qa/extras/odfexport/data/tdf99631.docx
Binary files differ
diff --git a/sw/qa/extras/odfexport/odfexport.cxx b/sw/qa/extras/odfexport/odfexport.cxx
index 930a3ce..4f13fc4 100644
--- a/sw/qa/extras/odfexport/odfexport.cxx
+++ b/sw/qa/extras/odfexport/odfexport.cxx
@@ -2123,6 +2123,21 @@ DECLARE_ODFEXPORT_TEST(tdf118502, "tdf118502.odt")
    CPPUNIT_ASSERT(xReplacementGraphic.is());
}

DECLARE_ODFEXPORT_TEST(tdf99631, "tdf99631.docx")
{
    // check import of VisualArea settings of the embedded XLSX OLE objects
    xmlDocPtr pXmlDoc = parseExport("Object 1/settings.xml");
    if (!pXmlDoc)
        return;
    assertXPathContent(pXmlDoc, "//config:config-item[@config:name='VisibleAreaWidth']", "4515");
    assertXPathContent(pXmlDoc, "//config:config-item[@config:name='VisibleAreaHeight']", "903");

    xmlDocPtr pXmlDoc2 = parseExport("Object 2/settings.xml");
    if (!pXmlDoc2)
        return;
    assertXPathContent(pXmlDoc2, "//config:config-item[@config:name='VisibleAreaWidth']", "4515");
    assertXPathContent(pXmlDoc2, "//config:config-item[@config:name='VisibleAreaHeight']", "1354");
}
#endif

CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/qa/extras/ooxmlexport/data/tdf99631.docx b/sw/qa/extras/ooxmlexport/data/tdf99631.docx
new file mode 100644
index 0000000..c72950b
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf99631.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport12.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport12.cxx
index 547afcd..3e90ed1 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport12.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport12.cxx
@@ -921,6 +921,21 @@ DECLARE_OOXMLEXPORT_TEST(testTdf117137, "tdf117137.docx")
    CPPUNIT_ASSERT(xPara3->getPropertyValue("NumberingRules").hasValue());
}

DECLARE_OOXMLEXPORT_TEST(testTdf99631, "tdf99631.docx")
{
    xmlDocPtr pXmlDoc = parseExport("word/document.xml");
    if (!pXmlDoc)
        return;
    assertXPath(pXmlDoc, "//w:object", 2);
    assertXPath(pXmlDoc, "/w:document/w:body/w:p[2]/w:r/w:object", 2);
    // first XSLX OLE object (1:1 scale)
    assertXPath(pXmlDoc, "/w:document/w:body/w:p[2]/w:r[1]/w:object[1]", "dxaOrig", "2560");
    assertXPath(pXmlDoc, "/w:document/w:body/w:p[2]/w:r[1]/w:object[1]", "dyaOrig", "513");
    // second XLSX OLE object (same content + 1 row, but zoomed)
    assertXPath(pXmlDoc, "/w:document/w:body/w:p[2]/w:r[2]/w:object[1]", "dxaOrig", "2560");
    assertXPath(pXmlDoc, "/w:document/w:body/w:p[2]/w:r[2]/w:object[1]", "dyaOrig", "768");
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/unocore/unoframe.cxx b/sw/source/core/unocore/unoframe.cxx
index 93cf268..c4d0e48 100644
--- a/sw/source/core/unocore/unoframe.cxx
+++ b/sw/source/core/unocore/unoframe.cxx
@@ -1127,7 +1127,9 @@ bool SwOLEProperties_Impl::AnyToItemSet(
{
    const ::uno::Any* pTemp;
    if(!GetProperty(FN_UNO_CLSID, 0, pTemp) && !GetProperty(FN_UNO_STREAM_NAME, 0, pTemp)
         && !GetProperty(FN_EMBEDDED_OBJECT, 0, pTemp) )
         && !GetProperty(FN_EMBEDDED_OBJECT, 0, pTemp)
         && !GetProperty(FN_UNO_VISIBLE_AREA_WIDTH, 0, pTemp)
         && !GetProperty(FN_UNO_VISIBLE_AREA_HEIGHT, 0, pTemp) )
        return false;
    SwFrameProperties_Impl::AnyToItemSet( pDoc, rFrameSet, rSet, rSizeFound);

@@ -1194,6 +1196,8 @@ SwXFrame::SwXFrame(FlyCntType eSet, const ::SfxItemPropertySet* pSet, SwDoc *pDo
    , eType(eSet)
    , bIsDescriptor(true)
    , m_nDrawAspect(embed::Aspects::MSOLE_CONTENT)
    , m_nVisibleAreaWidth(0)
    , m_nVisibleAreaHeight(0)
{
    // Register ourselves as a listener to the document (via the page descriptor)
    pDoc->getIDocumentStylePoolAccess().GetPageDescFromPool(RES_POOLPAGE_STANDARD)->Add(this);
@@ -1247,6 +1251,8 @@ SwXFrame::SwXFrame(SwFrameFormat& rFrameFormat, FlyCntType eSet, const ::SfxItem
    , eType(eSet)
    , bIsDescriptor(false)
    , m_nDrawAspect(embed::Aspects::MSOLE_CONTENT)
    , m_nVisibleAreaWidth(0)
    , m_nVisibleAreaHeight(0)
{
}

@@ -1949,6 +1955,18 @@ void SwXFrame::setPropertyValue(const OUString& rPropertyName, const ::uno::Any&
            else if (sAspect == "Content")
                m_nDrawAspect = embed::Aspects::MSOLE_CONTENT;
        }
        else if (FN_UNO_VISIBLE_AREA_WIDTH == pEntry->nWID)
        {
            OUString sAspect = "";
            aValue >>= sAspect;
            m_nVisibleAreaWidth = sAspect.toInt64();
        }
        else if (FN_UNO_VISIBLE_AREA_HEIGHT == pEntry->nWID)
        {
            OUString sAspect = "";
            aValue >>= sAspect;
            m_nVisibleAreaHeight = sAspect.toInt64();
        }
    }
    else
        throw uno::RuntimeException();
@@ -2845,6 +2863,21 @@ void SwXFrame::attachToRange(const uno::Reference< text::XTextRange > & xTextRan
            {
                UnoActionContext aAction(pDoc);
                pDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::INSERT, nullptr);

                // tdf#99631 set imported VisibleArea settings of embedded XLSX OLE objects
                if ( m_nDrawAspect == embed::Aspects::MSOLE_CONTENT
                        && m_nVisibleAreaWidth && m_nVisibleAreaHeight )
                {
                    sal_Int64 nAspect = m_nDrawAspect;
                    MapUnit aUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xIPObj->getMapUnit( nAspect ) );
                    Size aSize( OutputDevice::LogicToLogic(Size( m_nVisibleAreaWidth, m_nVisibleAreaHeight),
                        MapMode(MapUnit::MapTwip), MapMode(aUnit)));
                    awt::Size aSz;
                    aSz.Width = aSize.Width();
                    aSz.Height = aSize.Height();
                    xIPObj->setVisualAreaSize(m_nDrawAspect, aSz);
                }

                if(!bSizeFound)
                {
                    //TODO/LATER: how do I transport it to the OLENode?
diff --git a/sw/source/core/unocore/unomap1.cxx b/sw/source/core/unocore/unomap1.cxx
index de331a1..2c96d9f 100644
--- a/sw/source/core/unocore/unomap1.cxx
+++ b/sw/source/core/unocore/unomap1.cxx
@@ -899,6 +899,8 @@ const SfxItemPropertyMapEntry*  SwUnoPropertyMapProvider::GetEmbeddedPropertyMap
        { OUString(UNO_NAME_COMPONENT),FN_UNO_COMPONENT, cppu::UnoType<css::lang::XComponent>::get(), PropertyAttribute::READONLY, 0},
        { OUString(UNO_NAME_EMBEDDED_OBJECT),FN_EMBEDDED_OBJECT, cppu::UnoType<css::embed::XEmbeddedObject>::get(), PROPERTY_NONE, 0},
        { OUString(UNO_NAME_DRAW_ASPECT),FN_UNO_DRAW_ASPECT, cppu::UnoType<OUString>::get(), PROPERTY_NONE, 0 },
        { OUString(UNO_NAME_VISIBLE_AREA_WIDTH),FN_UNO_VISIBLE_AREA_WIDTH, cppu::UnoType<OUString>::get(), PROPERTY_NONE, 0 },
        { OUString(UNO_NAME_VISIBLE_AREA_HEIGHT),FN_UNO_VISIBLE_AREA_HEIGHT, cppu::UnoType<OUString>::get(), PROPERTY_NONE, 0 },
        // added FillProperties for SW, same as FILL_PROPERTIES in svx
        // but need own defines in Writer due to later association of strings
        // and uno types (see loop at end of this method and definition of SW_PROP_NMID)
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx
index c576fec..85e8167 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -147,6 +147,8 @@
#include <algorithm>
#include <stdarg.h>

#include <toolkit/helper/vclunohelper.hxx>

using ::editeng::SvxBorderLine;

using namespace oox;
@@ -5314,7 +5316,31 @@ void DocxAttributeOutput::WriteOLE( SwOLENode& rNode, const Size& rSize, const S
    m_rDrawingML.SetFS(m_pSerializer);
    OUString sImageId = m_rDrawingML.WriteImage( *pGraphic );

    m_pSerializer->startElementNS( XML_w, XML_object, FSEND );
    if ( sDrawAspect == "Content" )
    {
        awt::Size aSize;
        try
        {
            aSize = xObj->getVisualAreaSize( rNode.GetAspect() );

            MapUnit aUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( rNode.GetAspect() ) );
            Size aOriginalSize( OutputDevice::LogicToLogic(Size( aSize.Width, aSize.Height),
                                                MapMode(aUnit), MapMode(MapUnit::MapTwip)));

            m_pSerializer->startElementNS( XML_w, XML_object,
                                   FSNS(XML_w, XML_dxaOrig), OString::number(aOriginalSize.Width()),
                                   FSNS(XML_w, XML_dyaOrig), OString::number(aOriginalSize.Height()),
                                   FSEND );
        }
        catch ( uno::Exception& )
        {
            m_pSerializer->startElementNS( XML_w, XML_object, FSEND );
        }
    }
    else
    {
        m_pSerializer->startElementNS( XML_w, XML_object, FSEND );
    }

    OStringBuffer sShapeStyle, sShapeId;
    sShapeStyle.append( "width:" ).append( double( rSize.Width() ) / 20 )
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index f2a14ce..46ff798 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -1762,6 +1762,14 @@ void DomainMapper_Impl::appendOLE( const OUString& rStreamName, const std::share
        xOLEProperties->setPropertyValue(getPropertyName( PROP_HEIGHT ),
                        uno::makeAny(aSize.Height));

        OUString aVisAreaWidth = pOLEHandler->GetVisAreaWidth();
        if(!aVisAreaWidth.isEmpty())
            xOLEProperties->setPropertyValue("VisibleAreaWidth", uno::makeAny(aVisAreaWidth));

        OUString aVisAreaHeight = pOLEHandler->GetVisAreaHeight();
        if(!aVisAreaHeight.isEmpty())
            xOLEProperties->setPropertyValue("VisibleAreaHeight", uno::makeAny(aVisAreaHeight));

        uno::Reference< graphic::XGraphic > xGraphic = pOLEHandler->getReplacement();
        xOLEProperties->setPropertyValue(getPropertyName( PROP_GRAPHIC ),
                        uno::makeAny(xGraphic));
diff --git a/writerfilter/source/dmapper/OLEHandler.cxx b/writerfilter/source/dmapper/OLEHandler.cxx
index dee9ceb..fad4494 100644
--- a/writerfilter/source/dmapper/OLEHandler.cxx
+++ b/writerfilter/source/dmapper/OLEHandler.cxx
@@ -95,8 +95,10 @@ void OLEHandler::lcl_attribute(Id rName, Value & rVal)
            rVal.getAny() >>= m_xInputStream;
        break;
        case NS_ooxml::LN_CT_Object_dxaOrig:
             m_sVisAreaWidth = sStringValue;
        break;
        case NS_ooxml::LN_CT_Object_dyaOrig:
             m_sVisAreaHeight = sStringValue;
        break;
        case NS_ooxml::LN_shape:
        {
@@ -262,6 +264,16 @@ OUString const & OLEHandler::GetDrawAspect() const
    return m_sDrawAspect;
}

OUString const & OLEHandler::GetVisAreaWidth() const
{
    return m_sVisAreaWidth;
}

OUString const & OLEHandler::GetVisAreaHeight() const
{
    return m_sVisAreaHeight;
}

OUString OLEHandler::copyOLEOStream(
        uno::Reference<text::XTextDocument> const& xTextDocument)
{
diff --git a/writerfilter/source/dmapper/OLEHandler.hxx b/writerfilter/source/dmapper/OLEHandler.hxx
index 6ba2fb3..b0aae53 100644
--- a/writerfilter/source/dmapper/OLEHandler.hxx
+++ b/writerfilter/source/dmapper/OLEHandler.hxx
@@ -52,6 +52,8 @@ class OLEHandler : public LoggedProperties
    OUString     m_sProgId;
    OUString     m_sShapeId;
    OUString     m_sDrawAspect;
    OUString     m_sVisAreaWidth;
    OUString     m_sVisAreaHeight;
    OUString     m_sObjectId;
    OUString     m_sr_id;
    /// The stream URL right after the import of the raw data.
@@ -90,6 +92,8 @@ public:
    OUString getCLSID(const css::uno::Reference<css::uno::XComponentContext>& xComponentContext) const;

    OUString const & GetDrawAspect() const;
    OUString const & GetVisAreaWidth() const;
    OUString const & GetVisAreaHeight() const;

    OUString copyOLEOStream(css::uno::Reference<css::text::XTextDocument> const& xTextDocument);