vcl: add graphic export-as-pdf filter

In case the metafile was imported from a PDF originally, then this
allows storing the PDF stream next to the graphic. This means that e.g.
in Writer choosing 'Save' from the context menu of the graphic can write
the original PDF stream, not the replacement metafile.

Change-Id: I4ab45d5af17fe46d7538df6d79d6b57ed163572a
Reviewed-on: https://gerrit.libreoffice.org/26628
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
diff --git a/filter/Configuration_filter.mk b/filter/Configuration_filter.mk
index ef895de..44c4420 100644
--- a/filter/Configuration_filter.mk
+++ b/filter/Configuration_filter.mk
@@ -839,6 +839,7 @@ $(eval $(call filter_Configuration_add_internal_filters,fcfg_langpack,fcfg_inter
	pcd_Import_Base16 \
	pct_Import \
	pcx_Import \
	pdf_Export \
	pdf_Import \
	pgm_Import \
	png_Export \
diff --git a/filter/source/config/fragments/internalgraphicfilters/pdf_Export.xcu b/filter/source/config/fragments/internalgraphicfilters/pdf_Export.xcu
new file mode 100644
index 0000000..d82c771
--- /dev/null
+++ b/filter/source/config/fragments/internalgraphicfilters/pdf_Export.xcu
@@ -0,0 +1,17 @@
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
-->
    <node oor:name="pdf_Export" oor:op="replace"  >
        <prop oor:name="Type"><value>pdf_Portable_Document_Format</value></prop>
        <prop oor:name="FormatName"><value>SVEPDF</value></prop>
        <prop oor:name="RealFilterName"/>
        <prop oor:name="UIComponent"><value>com.sun.star.svtools.SvFilterOptionsDialog</value></prop>
        <prop oor:name="UIName">
            <value xml:lang="en-US">PDF - Portable Document Format</value>
        </prop>
        <prop oor:name="Flags"><value>EXPORT</value></prop>
    </node>
diff --git a/include/vcl/gfxlink.hxx b/include/vcl/gfxlink.hxx
index 0fc6e0c..0fcca06 100644
--- a/include/vcl/gfxlink.hxx
+++ b/include/vcl/gfxlink.hxx
@@ -42,11 +42,12 @@ enum class GfxLinkType
    NativePct    = 8,    // Don't forget to update the following defines
    NativeSvg    = 9,    // Don't forget to update the following defines
    NativeMov    = 10,   // Don't forget to update the following defines
    NativeBmp    = 11   // Don't forget to update the following defines
    NativeBmp    = 11,
    NativePdf    = 12    // Don't forget to update the following defines
};

#define GFX_LINK_FIRST_NATIVE_ID    GfxLinkType::NativeGif
#define GFX_LINK_LAST_NATIVE_ID     GfxLinkType::NativeBmp
#define GFX_LINK_LAST_NATIVE_ID     GfxLinkType::NativePdf

class Graphic;

diff --git a/include/vcl/graph.hxx b/include/vcl/graph.hxx
index cffafa7..697565d 100644
--- a/include/vcl/graph.hxx
+++ b/include/vcl/graph.hxx
@@ -220,6 +220,9 @@ public:

    const SvgDataPtr& getSvgData() const;

    void setPdfData(const css::uno::Sequence<sal_Int8>& rPdfData);
    const css::uno::Sequence<sal_Int8>& getPdfData() const;

    static css::uno::Sequence<sal_Int8> getUnoTunnelId();
};

diff --git a/include/vcl/graphicfilter.hxx b/include/vcl/graphicfilter.hxx
index 0a2f8d5..ff8353c 100644
--- a/include/vcl/graphicfilter.hxx
+++ b/include/vcl/graphicfilter.hxx
@@ -82,6 +82,7 @@ namespace o3tl
#define EXP_EMF                 "SVEMF"
#define EXP_JPEG                "SVEJPEG"
#define EXP_SVG                 "SVESVG"
#define EXP_PDF                 "SVEPDF"
#define EXP_PNG                 "SVEPNG"

#define BMP_SHORTNAME           "BMP"
diff --git a/svx/source/core/graphichelper.cxx b/svx/source/core/graphichelper.cxx
index f163af0..5be57e8 100644
--- a/svx/source/core/graphichelper.cxx
+++ b/svx/source/core/graphichelper.cxx
@@ -87,6 +87,9 @@ void GraphicHelper::GetPreferredExtension( OUString& rExtension, const Graphic& 
        case GfxLinkType::NativeSvg:
            aExtension = "svg";
            break;
        case GfxLinkType::NativePdf:
            aExtension = "pdf";
            break;
        default:
            break;
    }
diff --git a/svx/source/xoutdev/_xoutbmp.cxx b/svx/source/xoutdev/_xoutbmp.cxx
index 7dea733..592434b 100644
--- a/svx/source/xoutdev/_xoutbmp.cxx
+++ b/svx/source/xoutdev/_xoutbmp.cxx
@@ -39,6 +39,8 @@
#define FORMAT_JPG  "jpg"
#define FORMAT_PNG  "png"

using namespace com::sun::star;

GraphicFilter* XOutBitmap::pGrfFilter = nullptr;

Animation XOutBitmap::MirrorAnimation( const Animation& rAnimation, bool bHMirr, bool bVMirr )
@@ -172,6 +174,24 @@ sal_uInt16 XOutBitmap::WriteGraphic( const Graphic& rGraphic, OUString& rFileNam
            }
        }

        // Write PDF data in original form if possible.
        if (rGraphic.getPdfData().hasElements() && rFilterName.equalsIgnoreAsciiCase("pdf"))
        {
            if (!(nFlags & XOutFlags::DontAddExtension))
                aURL.setExtension(rFilterName);

            rFileName = aURL.GetMainURL(INetURLObject::NO_DECODE);
            SfxMedium aMedium(aURL.GetMainURL(INetURLObject::NO_DECODE), StreamMode::WRITE|StreamMode::SHARE_DENYNONE|StreamMode::TRUNC);
            if (SvStream* pOutStream = aMedium.GetOutStream())
            {
                uno::Sequence<sal_Int8> aPdfData = rGraphic.getPdfData();
                pOutStream->WriteBytes(aPdfData.getConstArray(), aPdfData.getLength());
                aMedium.Commit();
                if (!aMedium.GetError())
                    nErr = GRFILTER_OK;
            }
        }

        if( GRFILTER_OK != nErr )
        {
            if( ( nFlags & XOutFlags::UseNativeIfPossible ) &&
diff --git a/vcl/inc/impgraph.hxx b/vcl/inc/impgraph.hxx
index 5f26354..a41d684 100644
--- a/vcl/inc/impgraph.hxx
+++ b/vcl/inc/impgraph.hxx
@@ -51,6 +51,7 @@ private:
    bool                mbSwapUnderway;
    bool                mbDummyContext;
    SvgDataPtr          maSvgData;
    css::uno::Sequence<sal_Int8> maPdfData;

private:

diff --git a/vcl/source/filter/FilterConfigCache.cxx b/vcl/source/filter/FilterConfigCache.cxx
index d8d8862..262bd66 100644
--- a/vcl/source/filter/FilterConfigCache.cxx
+++ b/vcl/source/filter/FilterConfigCache.cxx
@@ -43,7 +43,7 @@ const char* FilterConfigCache::FilterConfigCacheEntry::InternalPixelFilterNameLi
const char* FilterConfigCache::FilterConfigCacheEntry::InternalVectorFilterNameList[] =
{
    IMP_SVMETAFILE, IMP_WMF, IMP_EMF, IMP_SVSGF, IMP_SVSGV, IMP_SVG, IMP_PDF,
    EXP_SVMETAFILE, EXP_WMF, EXP_EMF, EXP_SVG, nullptr
    EXP_SVMETAFILE, EXP_WMF, EXP_EMF, EXP_SVG, EXP_PDF, nullptr
};

const char* FilterConfigCache::FilterConfigCacheEntry::ExternalPixelFilterNameList[] =
diff --git a/vcl/source/filter/graphicfilter.cxx b/vcl/source/filter/graphicfilter.cxx
index 38e72dc..b65a8a0 100644
--- a/vcl/source/filter/graphicfilter.cxx
+++ b/vcl/source/filter/graphicfilter.cxx
@@ -1698,6 +1698,8 @@ sal_uInt16 GraphicFilter::ImportGraphic( Graphic& rGraphic, const OUString& rPat
        {
            if (!ImportPDF(rIStream, rGraphic))
                nStatus = GRFILTER_FILTERERROR;
            else
                eLinkType = GfxLinkType::NativePdf;
        }
        else
            nStatus = GRFILTER_FILTERERROR;
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
index cc63415..6bd1f6f 100644
--- a/vcl/source/filter/ipdf/pdfread.cxx
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -101,6 +101,13 @@ VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic)

    rGraphic = aMtf;

    // Save the original PDF stream for later use.
    rStream.Seek(STREAM_SEEK_TO_END);
    uno::Sequence<sal_Int8> aPdfData(rStream.Tell());
    rStream.Seek(STREAM_SEEK_TO_BEGIN);
    rStream.ReadBytes(aPdfData.getArray(), aPdfData.getLength());
    rGraphic.setPdfData(aPdfData);

    return true;
}

diff --git a/vcl/source/gdi/graph.cxx b/vcl/source/gdi/graph.cxx
index c5a7901e..7a61594 100644
--- a/vcl/source/gdi/graph.cxx
+++ b/vcl/source/gdi/graph.cxx
@@ -593,6 +593,17 @@ const SvgDataPtr& Graphic::getSvgData() const
    return mpImpGraphic->getSvgData();
}

void Graphic::setPdfData(const uno::Sequence<sal_Int8>& rPdfData)
{
    ImplTestRefCount();
    mpImpGraphic->maPdfData = rPdfData;
}

const uno::Sequence<sal_Int8>& Graphic::getPdfData() const
{
    return mpImpGraphic->maPdfData;
}

namespace {

struct Id: public rtl::Static<cppu::OImplementationId, Id> {};
diff --git a/vcl/source/gdi/impgraph.cxx b/vcl/source/gdi/impgraph.cxx
index 8c66d1a..933a0a6 100644
--- a/vcl/source/gdi/impgraph.cxx
+++ b/vcl/source/gdi/impgraph.cxx
@@ -51,6 +51,8 @@
#define GRAPHIC_FORMAT_50           static_cast<sal_uInt32>(COMPAT_FORMAT( 'G', 'R', 'F', '5' ))
#define NATIVE_FORMAT_50            static_cast<sal_uInt32>(COMPAT_FORMAT( 'N', 'A', 'T', '5' ))

using namespace com::sun::star;

struct ImpSwapFile
{
    INetURLObject   aSwapURL;
@@ -132,6 +134,7 @@ ImpGraphic::ImpGraphic( const ImpGraphic& rImpGraphic ) :
        mpAnimation = nullptr;

    maSvgData = rImpGraphic.maSvgData;
    maPdfData = rImpGraphic.maPdfData;
}

ImpGraphic::ImpGraphic( const Bitmap& rBitmap ) :
@@ -256,6 +259,7 @@ ImpGraphic& ImpGraphic::operator=( const ImpGraphic& rImpGraphic )
            mpGfxLink = nullptr;

        maSvgData = rImpGraphic.maSvgData;
        maPdfData = rImpGraphic.maPdfData;
    }

    return *this;
@@ -304,6 +308,10 @@ bool ImpGraphic::operator==( const ImpGraphic& rImpGraphic ) const
                        }
                    }
                }
                else if (maPdfData.hasElements())
                {
                    bRet = maPdfData == rImpGraphic.maPdfData;
                }
                else if( mpAnimation )
                {
                    if( rImpGraphic.mpAnimation && ( *rImpGraphic.mpAnimation == *mpAnimation ) )
@@ -349,6 +357,7 @@ void ImpGraphic::ImplClearGraphics( bool bCreateSwapInfo )
    }

    maSvgData.reset();
    maPdfData = uno::Sequence<sal_Int8>();
}

void ImpGraphic::ImplClear()