Resolves: #i119735# missing css.svg.SVGWriter when using GraphicProvider

missing implementation for com.sun.star.svg.SVGWriter when using
GraphicProvider

Adapted patch provided by Sven Jacobi (thanks), added some fixes/cleanups to
make it work with all kind of graphics (SVG, Bitmap, Metafile)

(cherry picked from commit 891011181285d7ad54984ffd05ed44d825a70647)

Conflicts:
	filter/inc/filter/msfilter/msdffimp.hxx
	filter/source/graphicfilter/epict/epict.cxx
	filter/source/svg/svgexport.cxx
	filter/source/svg/svguno.cxx
	filter/source/svg/svgwriter.cxx
	filter/source/svg/svgwriter.hxx
	svtools/source/filter/filter.cxx

Change-Id: Ie53f995614060a00c709f620f89d132913a25c5c
diff --git a/filter/source/graphicfilter/eos2met/eos2met.cxx b/filter/source/graphicfilter/eos2met/eos2met.cxx
index 1fe92ac..9e5ae3b 100644
--- a/filter/source/graphicfilter/eos2met/eos2met.cxx
+++ b/filter/source/graphicfilter/eos2met/eos2met.cxx
@@ -2560,20 +2560,8 @@ extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
GraphicExport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* pFilterConfigItem, sal_Bool )
{   METWriter aMETWriter;

    if ( rGraphic.GetType() == GRAPHIC_GDIMETAFILE )
        return aMETWriter.WriteMET( rGraphic.GetGDIMetaFile(), rStream, pFilterConfigItem );
    else
    {
        Bitmap aBmp=rGraphic.GetBitmap();
        GDIMetaFile aMTF;
        VirtualDevice aVirDev;

        aMTF.Record(&aVirDev);
        aVirDev.DrawBitmap(Point(),aBmp);
        aMTF.Stop();
        aMTF.SetPrefSize(aBmp.GetSizePixel());
        return aMETWriter.WriteMET( aMTF, rStream, pFilterConfigItem );
    }
    // #119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
    return aMETWriter.WriteMET( rGraphic.GetGDIMetaFile(), rStream, pFilterConfigItem );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/filter/source/graphicfilter/epict/epict.cxx b/filter/source/graphicfilter/epict/epict.cxx
index c68d400..3c02e0a 100644
--- a/filter/source/graphicfilter/epict/epict.cxx
+++ b/filter/source/graphicfilter/epict/epict.cxx
@@ -2275,23 +2275,10 @@ GraphicExport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem* pFilterC
{
    PictWriter      aPictWriter;

    if (rGraphic.GetType()==GRAPHIC_GDIMETAFILE)
    {
        GDIMetaFile aScaledMtf( rGraphic.GetGDIMetaFile() );
        return aPictWriter.WritePict( aScaledMtf, rStream, pFilterConfigItem );
    }
    else
    {
        Bitmap aBmp=rGraphic.GetBitmap();
        GDIMetaFile aMTF;
        VirtualDevice aVirDev;
    // #i119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
    GDIMetaFile aScaledMtf( rGraphic.GetGDIMetaFile() );

        aMTF.Record(&aVirDev);
        aVirDev.DrawBitmap(Point(),aBmp);
        aMTF.Stop();
        aMTF.SetPrefSize(aBmp.GetSizePixel());
        return aPictWriter.WritePict( aMTF, rStream, pFilterConfigItem );
    }
    return aPictWriter.WritePict( aScaledMtf, rStream, pFilterConfigItem );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/filter/source/svg/svgexport.cxx b/filter/source/svg/svgexport.cxx
index 918ef48..27a7125 100644
--- a/filter/source/svg/svgexport.cxx
+++ b/filter/source/svg/svgexport.cxx
@@ -2343,4 +2343,46 @@ IMPL_LINK( SVGFilter, CalcFieldHdl, EditFieldInfo*, pInfo )
    return ( bFieldProcessed ? 0 : maOldFieldHdl.Call( pInfo ) );
}

// -----------------------------------------------------------------------------

void SVGExport::writeMtf( const GDIMetaFile& rMtf )
{
    const Size aSize( OutputDevice::LogicToLogic( rMtf.GetPrefSize(), rMtf.GetPrefMapMode(), MAP_MM ) );
    rtl::OUString aAttr;
    Reference< XExtendedDocumentHandler> xExtDocHandler( GetDocHandler(), UNO_QUERY );

    if( xExtDocHandler.is() )
        xExtDocHandler->unknown( SVG_DTD_STRING );

    aAttr = rtl::OUString::valueOf( aSize.Width() );
    aAttr += "mm";
    AddAttribute( XML_NAMESPACE_NONE, "width", aAttr );

    aAttr = rtl::OUString::valueOf( aSize.Height() );
    aAttr += "mm";
    AddAttribute( XML_NAMESPACE_NONE, "height", aAttr );

    aAttr = "0 0 ";
    aAttr += rtl::OUString::valueOf( aSize.Width() * 100L );
    aAttr += " ";
    aAttr += rtl::OUString::valueOf( aSize.Height() * 100L );
    AddAttribute( XML_NAMESPACE_NONE, "viewBox", aAttr );

    {
        SvXMLElementExport  aSVG( *this, XML_NAMESPACE_NONE, "svg", sal_True, sal_True );

        std::vector< ObjectRepresentation > aObjects;

        aObjects.push_back( ObjectRepresentation( Reference< XInterface >(), rMtf ) );
        SVGFontExport aSVGFontExport( *this, aObjects );

        Point aPoint100thmm( OutputDevice::LogicToLogic( rMtf.GetPrefMapMode().GetOrigin(), rMtf.GetPrefMapMode(), MAP_100TH_MM ) );
        Size  aSize100thmm( OutputDevice::LogicToLogic( rMtf.GetPrefSize(), rMtf.GetPrefMapMode(), MAP_100TH_MM ) );

        SVGActionWriter aWriter( *this, aSVGFontExport );
        aWriter.WriteMetaFile( aPoint100thmm, aSize100thmm, rMtf,
            SVGWRITER_WRITE_FILL | SVGWRITER_WRITE_TEXT, NULL );
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/filter/source/svg/svgfilter.component b/filter/source/svg/svgfilter.component
index 51fd50d6..1bf7b47 100644
--- a/filter/source/svg/svgfilter.component
+++ b/filter/source/svg/svgfilter.component
@@ -24,4 +24,7 @@
    <service name="com.sun.star.document.ExportFilter"/>
    <service name="com.sun.star.document.ExtendedTypeDetection"/>
  </implementation>
  <implementation name="com.sun.star.comp.Draw.SVGWriter">
    <service name="com.sun.star.svg.SVGWriter"/>
  </implementation>
</component>
diff --git a/filter/source/svg/svgfilter.cxx b/filter/source/svg/svgfilter.cxx
index eb22854..7556840 100644
--- a/filter/source/svg/svgfilter.cxx
+++ b/filter/source/svg/svgfilter.cxx
@@ -43,6 +43,7 @@
#include <osl/mutex.hxx>

#include "svgfilter.hxx"
#include "svgwriter.hxx"

using namespace ::com::sun::star;

@@ -306,23 +307,39 @@ OUString SAL_CALL SVGFilter::detect( Sequence< PropertyValue >& io_rDescriptor )

// -----------------------------------------------------------------------------

#define SVG_FILTER_IMPL_NAME "com.sun.star.comp.Draw.SVGFilter"
#define SVG_WRITER_IMPL_NAME "com.sun.star.comp.Draw.SVGWriter"

namespace sdecl = comphelper::service_decl;
 sdecl::class_<SVGFilter> serviceImpl;
 sdecl::class_<SVGFilter> serviceFilterImpl;
 const sdecl::ServiceDecl svgFilter(
     serviceImpl,
     "com.sun.star.comp.Draw.SVGFilter",
     serviceFilterImpl,
     SVG_FILTER_IMPL_NAME,
     "com.sun.star.document.ImportFilter;"
     "com.sun.star.document.ExportFilter;"
     "com.sun.star.document.ExtendedTypeDetection" );

 sdecl::class_<SVGWriter> serviceWriterImpl;
 const sdecl::ServiceDecl svgWriter(
     serviceWriterImpl,
     SVG_WRITER_IMPL_NAME,
     "com.sun.star.svg.SVGWriter" );

// The C shared lib entry points
extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL svgfilter_component_getFactory(
    sal_Char const* pImplName,
    ::com::sun::star::lang::XMultiServiceFactory* pServiceManager,
    ::com::sun::star::registry::XRegistryKey* pRegistryKey )
{
    return component_getFactoryHelper( pImplName, pServiceManager,
        pRegistryKey, svgFilter );
    if ( rtl_str_compare (pImplName, SVG_FILTER_IMPL_NAME) == 0 )
    {
        return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey, svgFilter );
    }
    else if ( rtl_str_compare (pImplName, SVG_WRITER_IMPL_NAME) == 0 )
    {
        return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey, svgWriter );
    }
    return NULL;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/filter/source/svg/svgfilter.hxx b/filter/source/svg/svgfilter.hxx
index 3c8e5a8..b0a6546 100644
--- a/filter/source/svg/svgfilter.hxx
+++ b/filter/source/svg/svgfilter.hxx
@@ -119,6 +119,8 @@ public:
    sal_Bool IsUseNativeTextDecoration() const;
    sal_Bool IsUseOpacity() const;

    void writeMtf( const GDIMetaFile& rMtf );

protected:

    virtual void            _ExportStyles( sal_Bool /* bUsed */ ) {}
diff --git a/filter/source/svg/svgwriter.cxx b/filter/source/svg/svgwriter.cxx
index cbb33e3..80cd9ca 100644
--- a/filter/source/svg/svgwriter.cxx
+++ b/filter/source/svg/svgwriter.cxx
@@ -2679,7 +2679,10 @@ void SVGActionWriter::ImplWriteText( const Point& rPos, const String& rText,
                        if( bCont )
                        {
                            // #118796# do NOT access pDXArray, it may be zero (!)
                            nX = aPos.X() + pDX[ nCurPos - 1 ];
                            sal_Int32 nDXWidth = pDX[ nCurPos - 1 ];
                            if ( bApplyMapping )
                                nDXWidth = ImplMap( nDXWidth );
                            nX = aPos.X() + nDXWidth;
                        }
                    }
                }
@@ -3728,4 +3731,37 @@ void SVGActionWriter::WriteMetaFile( const Point& rPos100thmm,
    mpVDev->Pop();
}

// -------------
// - SVGWriter -
// -------------

SVGWriter::SVGWriter( const Reference< XComponentContext >& rxCtx )
    : mxContext(rxCtx)
{
}

// -----------------------------------------------------------------------------

SVGWriter::~SVGWriter()
{
}

// -----------------------------------------------------------------------------

void SAL_CALL SVGWriter::write( const Reference<XDocumentHandler>& rxDocHandler,
                                const Sequence<sal_Int8>& rMtfSeq ) throw( RuntimeException )
{
    SvMemoryStream  aMemStm( (char*) rMtfSeq.getConstArray(), rMtfSeq.getLength(), STREAM_READ );
    GDIMetaFile     aMtf;

    aMemStm >> aMtf;

    const Reference< XDocumentHandler > xDocumentHandler( rxDocHandler );
    const Sequence< PropertyValue > aFilterData;

    SVGExport* pWriter = new SVGExport( mxContext, xDocumentHandler, aFilterData );
    pWriter->writeMtf( aMtf );
    delete pWriter;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/filter/source/svg/svgwriter.hxx b/filter/source/svg/svgwriter.hxx
index dbce634..31ef423 100644
--- a/filter/source/svg/svgwriter.hxx
+++ b/filter/source/svg/svgwriter.hxx
@@ -21,7 +21,7 @@
#define SVGWRITER_HXX

#include <stack>
#include <cppuhelper/weak.hxx>
#include <cppuhelper/implbase1.hxx>
#include <rtl/ustring.hxx>
#include <tools/stream.hxx>
#include <tools/string.hxx>
@@ -58,6 +58,7 @@
#include <com/sun/star/text/XTextRange.hpp>
#include <com/sun/star/text/XTextField.hpp>
#include <com/sun/star/style/NumberingType.hpp>
#include <com/sun/star/svg/XSVGWriter.hpp>


// -----------------------------------------------------------------------------
@@ -68,6 +69,8 @@ using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::text;
using namespace ::com::sun::star::drawing;
using namespace ::com::sun::star::style;
using namespace ::com::sun::star::svg;
using namespace ::com::sun::star::xml::sax;

// -----------------------------------------------------------------------------

@@ -152,6 +155,7 @@ struct SVGShapeDescriptor


class SVGAttributeWriter;
class SVGExport;
class GDIMetaFile;


@@ -400,6 +404,22 @@ public:
                                           const GDIMetaFile* pTextEmbeddedBitmapMtf = NULL );
};

class SVGWriter : public cppu::WeakImplHelper1< XSVGWriter >
{
private:
    Reference< XComponentContext >      mxContext;

    SVGWriter();

public:
    explicit SVGWriter( const Reference< XComponentContext >& rxCtx );
    virtual ~SVGWriter();

    // XSVGWriter
    virtual void SAL_CALL write( const Reference<XDocumentHandler>& rxDocHandler,
        const Sequence<sal_Int8>& rMtfSeq ) throw( RuntimeException );
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/svdraw/svdxcgv.cxx b/svx/source/svdraw/svdxcgv.cxx
index 72268f6..6ed282d 100644
--- a/svx/source/svdraw/svdxcgv.cxx
+++ b/svx/source/svdraw/svdxcgv.cxx
@@ -504,16 +504,8 @@ GDIMetaFile SdrExchangeView::GetMarkedObjMetaFile(bool bNoVDevIfOneMtfMarked) co
            {
                Graphic aGraphic( pGrafObj->GetTransformedGraphic() );

                if( aGraphic.GetType() == GRAPHIC_BITMAP )
                {
                    const Point aPos;

                    aMtf.AddAction( new MetaBmpExScaleAction( aPos, aBoundSize, aGraphic.GetBitmapEx() ) );
                    aMtf.SetPrefMapMode( aMap );
                    aMtf.SetPrefSize( aBoundSize );
                }
                else
                    aMtf = aGraphic.GetGDIMetaFile();
                // #119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                aMtf = aGraphic.GetGDIMetaFile();
            }
        }

diff --git a/svx/source/unodraw/unoshap4.cxx b/svx/source/unodraw/unoshap4.cxx
index bacf34b..6c3d922 100644
--- a/svx/source/unodraw/unoshap4.cxx
+++ b/svx/source/unodraw/unoshap4.cxx
@@ -236,18 +236,8 @@ bool SvxOle2Shape::getPropertyValueImpl( const OUString& rName, const SfxItemPro
                }
                if ( !bIsWMF )
                {
                    GDIMetaFile aMtf;
                    if ( pGraphic->GetType() != GRAPHIC_BITMAP )
                        aMtf = pObj->GetGraphic()->GetGDIMetaFile();
                    else
                    {
                        VirtualDevice aVirDev;
                        aMtf.Record( &aVirDev );
                        pGraphic->Draw( &aVirDev, Point(),  pGraphic->GetPrefSize() );
                        aMtf.Stop();
                        aMtf.SetPrefSize( pGraphic->GetPrefSize() );
                        aMtf.SetPrefMapMode( pGraphic->GetPrefMapMode() );
                    }
                    // #i119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                    GDIMetaFile aMtf(pObj->GetGraphic()->GetGDIMetaFile());
                    SvMemoryStream aDestStrm( 65535, 65535 );
                    ConvertGDIMetaFileToWMF( aMtf, aDestStrm, NULL, sal_False );
                    const uno::Sequence<sal_Int8> aSeq(
diff --git a/svx/source/unodraw/unoshape.cxx b/svx/source/unodraw/unoshape.cxx
index a60030d..245d101 100644
--- a/svx/source/unodraw/unoshape.cxx
+++ b/svx/source/unodraw/unoshape.cxx
@@ -2942,18 +2942,8 @@ bool SvxShape::getPropertyValueImpl( const OUString&, const SfxItemPropertySimpl
                }
                if ( !bIsWMF )
                {
                    GDIMetaFile aMtf;
                    if ( pGraphic->GetType() != GRAPHIC_BITMAP )
                        aMtf = pObj->GetGraphic()->GetGDIMetaFile();
                    else
                    {
                        VirtualDevice aVirDev;
                        aMtf.Record( &aVirDev );
                        pGraphic->Draw( &aVirDev, Point(),  pGraphic->GetPrefSize() );
                        aMtf.Stop();
                        aMtf.SetPrefSize( pGraphic->GetPrefSize() );
                        aMtf.SetPrefMapMode( pGraphic->GetPrefMapMode() );
                    }
                    // #119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                    GDIMetaFile aMtf(pObj->GetGraphic()->GetGDIMetaFile());
                    SvMemoryStream aDestStrm( 65535, 65535 );
                    ConvertGDIMetaFileToWMF( aMtf, aDestStrm, NULL, sal_False );
                    const uno::Sequence<sal_Int8> aSeq(
diff --git a/vcl/source/filter/graphicfilter.cxx b/vcl/source/filter/graphicfilter.cxx
index ecfd469..eb86104 100644
--- a/vcl/source/filter/graphicfilter.cxx
+++ b/vcl/source/filter/graphicfilter.cxx
@@ -1902,69 +1902,30 @@ sal_uInt16 GraphicFilter::ExportGraphic( const Graphic& rGraphic, const String& 
                sal_Int32 nVersion = aConfigItem.ReadInt32( "Version", 0 ) ;
                if ( nVersion )
                    rOStm.SetVersion( nVersion );
                GDIMetaFile aMTF;

                if ( eType != GRAPHIC_BITMAP )
                    aMTF = aGraphic.GetGDIMetaFile();
                else
                {
                    VirtualDevice aVirDev;
                // #i119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                GDIMetaFile aMTF(aGraphic.GetGDIMetaFile());

                    aMTF.Record( &aVirDev );
                    aGraphic.Draw( &aVirDev, Point(), aGraphic.GetPrefSize() );
                    aMTF.Stop();
                    aMTF.SetPrefSize( aGraphic.GetPrefSize() );
                    aMTF.SetPrefMapMode( aGraphic.GetPrefMapMode() );
                }
                aMTF.Write( rOStm );

                if( rOStm.GetError() )
                    nStatus = GRFILTER_IOERROR;
            }
            else if ( aFilterName.EqualsIgnoreCaseAscii( EXP_WMF ) )
            {
                if( eType == GRAPHIC_GDIMETAFILE )
                {
                    if ( !ConvertGDIMetaFileToWMF( aGraphic.GetGDIMetaFile(), rOStm, &aConfigItem ) )
                        nStatus = GRFILTER_FORMATERROR;
                }
                else
                {
                    Bitmap          aBmp( aGraphic.GetBitmap() );
                    GDIMetaFile     aMTF;
                    VirtualDevice   aVirDev;
                // #i119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                if ( !ConvertGDIMetaFileToWMF( aGraphic.GetGDIMetaFile(), rOStm, &aConfigItem ) )
                    nStatus = GRFILTER_FORMATERROR;

                    aMTF.Record( &aVirDev );
                    aVirDev.DrawBitmap( Point(), aBmp );
                    aMTF.Stop();
                    aMTF.SetPrefSize( aBmp.GetSizePixel() );

                    if( !ConvertGDIMetaFileToWMF( aMTF, rOStm, &aConfigItem ) )
                        nStatus = GRFILTER_FORMATERROR;
                }
                if( rOStm.GetError() )
                    nStatus = GRFILTER_IOERROR;
            }
            else if ( aFilterName.EqualsIgnoreCaseAscii( EXP_EMF ) )
            {
                if( eType == GRAPHIC_GDIMETAFILE )
                {
                    if ( !ConvertGDIMetaFileToEMF( aGraphic.GetGDIMetaFile(), rOStm, &aConfigItem ) )
                        nStatus = GRFILTER_FORMATERROR;
                }
                else
                {
                    Bitmap          aBmp( aGraphic.GetBitmap() );
                    GDIMetaFile     aMTF;
                    VirtualDevice   aVirDev;
                // #i119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                if ( !ConvertGDIMetaFileToEMF( aGraphic.GetGDIMetaFile(), rOStm, &aConfigItem ) )
                    nStatus = GRFILTER_FORMATERROR;

                    aMTF.Record( &aVirDev );
                    aVirDev.DrawBitmap( Point(), aBmp );
                    aMTF.Stop();
                    aMTF.SetPrefSize( aBmp.GetSizePixel() );

                    if( !ConvertGDIMetaFileToEMF( aMTF, rOStm, &aConfigItem ) )
                        nStatus = GRFILTER_FORMATERROR;
                }
                if( rOStm.GetError() )
                    nStatus = GRFILTER_IOERROR;
            }
@@ -2080,6 +2041,7 @@ sal_uInt16 GraphicFilter::ExportGraphic( const Graphic& rGraphic, const String& 

                                    SvMemoryStream aMemStm( 65535, 65535 );

                                    // #i119735# just use GetGDIMetaFile, it will create a bufferd version of contained bitmap now automatically
                                    ( (GDIMetaFile&) aGraphic.GetGDIMetaFile() ).Write( aMemStm );

                                    xActiveDataSource->setOutputStream( ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >(
diff --git a/vcl/source/gdi/impgraph.cxx b/vcl/source/gdi/impgraph.cxx
index d980fc5..be56637 100644
--- a/vcl/source/gdi/impgraph.cxx
+++ b/vcl/source/gdi/impgraph.cxx
@@ -595,6 +595,40 @@ Animation ImpGraphic::ImplGetAnimation() const

const GDIMetaFile& ImpGraphic::ImplGetGDIMetaFile() const
{
    if (GRAPHIC_BITMAP == meType && !maMetaFile.GetActionSize())
    {
        // #i119735#
        // Use the local maMetaFile as container for a metafile-representation
        // of the bitmap graphic. This will be done only once, thus be buffered.
        // I checked all usages of maMetaFile, it is only used when type is not
        // GRAPHIC_BITMAP. In operator= it will get copied, thus buffering will
        // survive copying (change this if not wanted)
        ImpGraphic* pThat = const_cast< ImpGraphic* >(this);

        if(maSvgData.get() && !maEx)
        {
            // use maEx as local buffer for rendered svg
            pThat->maEx = maSvgData->getReplacement();
        }

        VirtualDevice aVirDev;
        const Size aSizePixel(maEx.GetSizePixel());

        pThat->maMetaFile.Record(&aVirDev);

        if(maEx.IsTransparent())
        {
            aVirDev.DrawBitmapEx(Point(), maEx);
        }
        else
        {
            aVirDev.DrawBitmap(Point(), maEx.GetBitmap());
        }

        pThat->maMetaFile.Stop();
        pThat->maMetaFile.SetPrefSize(aSizePixel);
    }

    return maMetaFile;
}