tdf#118977 support PDF documents as a clipboard format

On macOS it is possible to copy from a PDF a subset of a document,
which will be transported as a new PDF document containing the
subset.
LibreOffice didn't support PDF as a valid clipboard format and
previously it also didn't support showing PDFs inside the document,
so in such cases it copy-pasted a low resolution bitmap. The result
wasn't good.

As we are now able to display PDF documents as Graphic in LO, we
can also support this use-case. This adds support for the PDF
documents as a clipboard format in general and to the macOS
backend. This commit only adds support for Writer.

Reviewed-on: https://gerrit.libreoffice.org/71364
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit 615202cab983f1f13407f6515aeb047e4e09f6ed)

Change-Id: Ib982b55391b390ae06974b4ad836e376dd722a4c
Reviewed-on: https://gerrit.libreoffice.org/71575
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
diff --git a/include/sot/formats.hxx b/include/sot/formats.hxx
index 968af52..202c03b 100644
--- a/include/sot/formats.hxx
+++ b/include/sot/formats.hxx
@@ -174,8 +174,9 @@
    JPEG                   = 144,
    RICHTEXT               = 145,
    STRING_TSVC            = 146,
    PDF                    = 147,
    // the point at which we start allocating "runtime" format IDs
    USER_END  = STRING_TSVC
    USER_END  = PDF
};

/** Make it easier to iterate over format IDs */
diff --git a/sot/source/base/exchange.cxx b/sot/source/base/exchange.cxx
index cb9c502..99f941e 100644
--- a/sot/source/base/exchange.cxx
+++ b/sot/source/base/exchange.cxx
@@ -204,7 +204,8 @@
            /*143 SotClipboardFormatId::MATHML*/   { "application/mathml+xml", "MathML", &::cppu::UnoType<const Sequence< sal_Int8 >>::get() },
            /*144 SotClipboardFormatId::JPEG*/ { "image/jpeg", "JPEG Bitmap", &cppu::UnoType<Sequence<sal_Int8>>::get() },
            /*145 SotClipboardFormatId::RICHTEXT*/ { "text/richtext", "Richtext Format", &cppu::UnoType<Sequence<sal_Int8>>::get() },
            /*146 SotClipboardFormatId::STRING_TSVC*/            { "application/x-libreoffice-tsvc", "Text TSV-Calc", &cppu::UnoType<OUString>::get() }
            /*146 SotClipboardFormatId::STRING_TSVC*/            { "application/x-libreoffice-tsvc", "Text TSV-Calc", &cppu::UnoType<OUString>::get() },
            /*147 SotClipboardFormatId::PDF*/            { "application/pdf", "PDF Document", &cppu::UnoType<Sequence<sal_Int8>>::get() },
            };
        return &aInstance[0];
        }
diff --git a/sot/source/base/formats.cxx b/sot/source/base/formats.cxx
index 9b7770f..b3199f3 100644
--- a/sot/source/base/formats.cxx
+++ b/sot/source/base/formats.cxx
@@ -18,6 +18,7 @@
 */

#include <tools/solar.h>
#include <config_features.h>

#include <sot/exchange.hxx>
#include <sot/formats.hxx>
@@ -335,6 +336,9 @@
{
    { SotClipboardFormatId::GDIMETAFILE, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::DRAWING, EXCHG_IN_ACTION_COPY },
#if HAVE_FEATURE_PDFIUM
    { SotClipboardFormatId::PDF, EXCHG_IN_ACTION_COPY },
#endif
    { SotClipboardFormatId::PNG, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::JPEG, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::BITMAP, EXCHG_IN_ACTION_COPY },
@@ -355,6 +359,9 @@
    { SotClipboardFormatId::DRAWING, EXCHG_OUT_ACTION_REPLACE_DRAWOBJ, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::KeepPosSize | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::SVXB, EXCHG_OUT_ACTION_REPLACE_SVXB, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::KeepPosSize | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::GDIMETAFILE, EXCHG_OUT_ACTION_REPLACE_GDIMETAFILE, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::KeepPosSize | SotExchangeActionFlags::InsertTargetUrl, 0 },
#if HAVE_FEATURE_PDFIUM
    { SotClipboardFormatId::PDF, EXCHG_OUT_ACTION_INSERT_GRAPH, SotExchangeActionFlags::InsertImageMap  | SotExchangeActionFlags::InsertTargetUrl, 0 },
#endif
    { SotClipboardFormatId::PNG, EXCHG_OUT_ACTION_REPLACE_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::KeepPosSize | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::JPEG, EXCHG_OUT_ACTION_REPLACE_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::KeepPosSize | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::BITMAP, EXCHG_OUT_ACTION_REPLACE_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::KeepPosSize | SotExchangeActionFlags::InsertTargetUrl, 0 },
@@ -370,6 +377,9 @@
    { SotClipboardFormatId::DRAWING, EXCHG_OUT_ACTION_INSERT_DRAWOBJ, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::SVXB, EXCHG_OUT_ACTION_INSERT_SVXB, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::GDIMETAFILE, EXCHG_OUT_ACTION_INSERT_GDIMETAFILE, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
#if HAVE_FEATURE_PDFIUM
    { SotClipboardFormatId::PDF, EXCHG_OUT_ACTION_INSERT_GRAPH, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
#endif
    { SotClipboardFormatId::PNG, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::JPEG, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::BITMAP, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
@@ -827,6 +837,9 @@
    { SotClipboardFormatId::DRAWING, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::SVXB, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::GDIMETAFILE, EXCHG_IN_ACTION_COPY },
#if HAVE_FEATURE_PDFIUM
    { SotClipboardFormatId::PDF, EXCHG_IN_ACTION_COPY },
#endif
    { SotClipboardFormatId::PNG, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::JPEG, EXCHG_IN_ACTION_COPY },
    { SotClipboardFormatId::BITMAP, EXCHG_IN_ACTION_COPY },
@@ -857,6 +870,9 @@
    { SotClipboardFormatId::RICHTEXT, EXCHG_IN_ACTION_COPY, SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::STRING, EXCHG_OUT_ACTION_INSERT_STRING },
    { SotClipboardFormatId::GDIMETAFILE, EXCHG_OUT_ACTION_INSERT_GDIMETAFILE, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
#if HAVE_FEATURE_PDFIUM
    { SotClipboardFormatId::PDF, EXCHG_OUT_ACTION_INSERT_GRAPH, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
#endif
    { SotClipboardFormatId::PNG, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::JPEG, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::BITMAP, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
@@ -884,6 +900,9 @@
#ifndef MACOSX
    { SotClipboardFormatId::RTF, EXCHG_IN_ACTION_COPY, SotExchangeActionFlags::InsertTargetUrl, 0 },
#endif
#if HAVE_FEATURE_PDFIUM
    { SotClipboardFormatId::PDF, EXCHG_OUT_ACTION_INSERT_GRAPH, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
#endif
    { SotClipboardFormatId::PNG, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::JPEG, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
    { SotClipboardFormatId::BITMAP, EXCHG_OUT_ACTION_INSERT_BITMAP, SotExchangeActionFlags::InsertImageMap | SotExchangeActionFlags::InsertTargetUrl, 0 },
diff --git a/vcl/osx/DataFlavorMapping.cxx b/vcl/osx/DataFlavorMapping.cxx
index de8dc8c..6ccc2b8 100644
--- a/vcl/osx/DataFlavorMapping.cxx
+++ b/vcl/osx/DataFlavorMapping.cxx
@@ -113,6 +113,7 @@
    {
      { NSPasteboardTypeString, "text/plain;charset=utf-16", "Unicode Text (UTF-16)", true },
      { NSPasteboardTypeRTF, "text/rtf", "Rich Text Format", false },
      { NSPasteboardTypePDF, "application/pdf", "PDF File", false },
      { NSPasteboardTypeTIFF, "image/png", "Portable Network Graphics", false },
      { NSPasteboardTypeHTML, "text/html", "Plain Html", false },
SAL_WNODEPRECATED_DECLARATIONS_PUSH
diff --git a/vcl/source/treelist/transfer.cxx b/vcl/source/treelist/transfer.cxx
index 28fe43f..5dfb5c6 100644
--- a/vcl/source/treelist/transfer.cxx
+++ b/vcl/source/treelist/transfer.cxx
@@ -1686,6 +1686,20 @@
        if( bRet )
            rGraphic = aBmpEx;
    }
    else if(SotExchange::GetFormatDataFlavor(SotClipboardFormatId::PDF, aFlavor) &&
            TransferableDataHelper::IsEqual(aFlavor, rFlavor))
    {
        Graphic aGraphic;
        tools::SvRef<SotStorageStream> xStm;
        if (GetSotStorageStream(rFlavor, xStm))
        {
            if (GraphicConverter::Import(*xStm, aGraphic) == ERRCODE_NONE)
            {
                rGraphic = aGraphic;
                bRet = true;
            }
        }
    }
    else if (SotExchange::GetFormatDataFlavor(SotClipboardFormatId::JPEG, aFlavor) && TransferableDataHelper::IsEqual(aFlavor, rFlavor))
    {
        BitmapEx aBitmapEx;