[Libreoffice-commits] core.git: filter/Configuration_filter.mk filter/source include/vcl vcl/Library_vcl.mk vcl/source

Miklos Vajna vmiklos at collabora.co.uk
Thu Jun 23 15:25:31 UTC 2016


 filter/Configuration_filter.mk                                       |    1 
 filter/source/config/fragments/internalgraphicfilters/pdf_Import.xcu |   17 +
 include/vcl/graphicfilter.hxx                                        |    1 
 vcl/Library_vcl.mk                                                   |    1 
 vcl/source/filter/FilterConfigCache.cxx                              |    2 
 vcl/source/filter/graphicfilter.cxx                                  |   16 +
 vcl/source/filter/ipdf/pdfread.cxx                                   |  107 ++++++++++
 vcl/source/filter/ipdf/pdfread.hxx                                   |   21 +
 8 files changed, 165 insertions(+), 1 deletion(-)

New commits:
commit 878a860dff10bd91491d6c9f2f4e2308bfe4f0b2
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Thu Jun 23 12:53:54 2016 +0200

    vcl: add initial PDF import-as-graphic filter
    
    This allows Insert -> Image e.g. in Writer to read a PDF file, and
    insert the metafile equivalent of the first page into the document.
    
    Currently the original PDF document is lost on import (unlike when
    inserting an SVG file).
    
    Change-Id: Ib0472c5d9bd9a1da054353fa3a3a638a1052721e
    Reviewed-on: https://gerrit.libreoffice.org/26586
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>
    Tested-by: Jenkins <ci at libreoffice.org>

diff --git a/filter/Configuration_filter.mk b/filter/Configuration_filter.mk
index 5dd20f7..a17a00c 100644
--- a/filter/Configuration_filter.mk
+++ b/filter/Configuration_filter.mk
@@ -837,6 +837,7 @@ $(eval $(call filter_Configuration_add_internal_filters,fcfg_langpack,fcfg_inter
 	pcd_Import_Base16 \
 	pct_Import \
 	pcx_Import \
+	pdf_Import \
 	pgm_Import \
 	png_Export \
 	png_Import \
diff --git a/filter/source/config/fragments/internalgraphicfilters/pdf_Import.xcu b/filter/source/config/fragments/internalgraphicfilters/pdf_Import.xcu
new file mode 100644
index 0000000..a1cc9ce
--- /dev/null
+++ b/filter/source/config/fragments/internalgraphicfilters/pdf_Import.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_Import" oor:op="replace"  >
+        <prop oor:name="Type"><value>pdf_Portable_Document_Format</value></prop>
+        <prop oor:name="FormatName"><value>SVIPDF</value></prop>
+        <prop oor:name="RealFilterName"><value>PDF - Portable Document Format</value></prop>
+        <prop oor:name="UIComponent"/>
+        <prop oor:name="UIName">
+            <value xml:lang="en-US">PDF - Portable Document Format</value>
+        </prop>
+        <prop oor:name="Flags"><value>IMPORT</value></prop>
+    </node>
diff --git a/include/vcl/graphicfilter.hxx b/include/vcl/graphicfilter.hxx
index 4dc7ed5..4f32b17 100644
--- a/include/vcl/graphicfilter.hxx
+++ b/include/vcl/graphicfilter.hxx
@@ -75,6 +75,7 @@ namespace o3tl
 #define IMP_XBM                 "SVIXBM"
 #define IMP_XPM                 "SVIXPM"
 #define IMP_SVG                 "SVISVG"
+#define IMP_PDF                 "SVIPDF"
 #define EXP_BMP                 "SVBMP"
 #define EXP_SVMETAFILE          "SVMETAFILE"
 #define EXP_WMF                 "SVWMF"
diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk
index 78f38cf..d3c3f80 100644
--- a/vcl/Library_vcl.mk
+++ b/vcl/Library_vcl.mk
@@ -362,6 +362,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\
     vcl/source/filter/sgvtext \
     vcl/source/filter/igif/decode \
     vcl/source/filter/igif/gifread \
+    vcl/source/filter/ipdf/pdfread \
     vcl/source/filter/ixbm/xbmread \
     vcl/source/filter/ixpm/xpmread \
     vcl/source/filter/jpeg/Exif \
diff --git a/vcl/source/filter/FilterConfigCache.cxx b/vcl/source/filter/FilterConfigCache.cxx
index dce03d7..d8d8862 100644
--- a/vcl/source/filter/FilterConfigCache.cxx
+++ b/vcl/source/filter/FilterConfigCache.cxx
@@ -42,7 +42,7 @@ const char* FilterConfigCache::FilterConfigCacheEntry::InternalPixelFilterNameLi
 
 const char* FilterConfigCache::FilterConfigCacheEntry::InternalVectorFilterNameList[] =
 {
-    IMP_SVMETAFILE, IMP_WMF, IMP_EMF, IMP_SVSGF, IMP_SVSGV, IMP_SVG,
+    IMP_SVMETAFILE, IMP_WMF, IMP_EMF, IMP_SVSGF, IMP_SVSGV, IMP_SVG, IMP_PDF,
     EXP_SVMETAFILE, EXP_WMF, EXP_EMF, EXP_SVG, nullptr
 };
 
diff --git a/vcl/source/filter/graphicfilter.cxx b/vcl/source/filter/graphicfilter.cxx
index da9d790..36c05eb 100644
--- a/vcl/source/filter/graphicfilter.cxx
+++ b/vcl/source/filter/graphicfilter.cxx
@@ -41,6 +41,7 @@
 #include <vcl/wmf.hxx>
 #include <vcl/settings.hxx>
 #include "igif/gifread.hxx"
+#include "ipdf/pdfread.hxx"
 #include "jpeg/jpeg.hxx"
 #include "ixbm/xbmread.hxx"
 #include "ixpm/xpmread.hxx"
@@ -763,6 +764,16 @@ static bool ImpPeekGraphicFormat( SvStream& rStream, OUString& rFormatExtension,
         }
     }
 
+    if (!bTest || rFormatExtension.startsWith("PDF"))
+    {
+        if ((sFirstBytes[0] == '%' && sFirstBytes[1] == 'P' && sFirstBytes[2] == 'D' &&
+             sFirstBytes[3] == 'F' && sFirstBytes[4] == '-'))
+        {
+            rFormatExtension = "PDF";
+            return true;
+        }
+    }
+
     return bTest && !bSomethingTested;
 }
 
@@ -1685,6 +1696,11 @@ sal_uInt16 GraphicFilter::ImportGraphic( Graphic& rGraphic, const OUString& rPat
                 break;
             }
         }
+        else if (aFilterName == IMP_PDF)
+        {
+            if (!ImportPDF(rIStream, rGraphic))
+                nStatus = GRFILTER_FILTERERROR;
+        }
         else
             nStatus = GRFILTER_FILTERERROR;
     }
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
new file mode 100644
index 0000000..cc63415
--- /dev/null
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -0,0 +1,107 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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/.
+ */
+
+#include "pdfread.hxx"
+
+#include <com/sun/star/beans/XPropertySet.hpp>
+#include <com/sun/star/document/XFilter.hpp>
+#include <com/sun/star/document/XImporter.hpp>
+#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
+#include <com/sun/star/frame/Desktop.hpp>
+#include <com/sun/star/io/XStream.hpp>
+#include <com/sun/star/lang/XMultiServiceFactory.hpp>
+
+#include <comphelper/processfactory.hxx>
+#include <comphelper/propertyvalue.hxx>
+#include <comphelper/scopeguard.hxx>
+#include <unotools/streamwrap.hxx>
+#include <vcl/wmf.hxx>
+
+using namespace com::sun::star;
+
+namespace
+{
+
+/// Imports a PDF stream into Draw.
+uno::Reference<lang::XComponent> importIntoDraw(SvStream& rStream)
+{
+    // Create an empty Draw component.
+    uno::Reference<frame::XDesktop2> xDesktop = css::frame::Desktop::create(comphelper::getProcessComponentContext());
+    uno::Reference<frame::XComponentLoader> xComponentLoader(xDesktop, uno::UNO_QUERY);
+    uno::Sequence<beans::PropertyValue> aLoadArguments =
+    {
+        comphelper::makePropertyValue("Hidden", true)
+    };
+    uno::Reference<lang::XComponent> xComponent = xComponentLoader->loadComponentFromURL("private:factory/sdraw", "_default", 0, aLoadArguments);
+
+    // Import the PDF into it.
+    uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
+    // Need to go via FilterFactory, otherwise XmlFilterAdaptor::initialize() is not called.
+    uno::Reference<lang::XMultiServiceFactory> xFilterFactory(xMultiServiceFactory->createInstance("com.sun.star.document.FilterFactory"), uno::UNO_QUERY);
+    uno::Reference<document::XFilter> xFilter(xFilterFactory->createInstanceWithArguments("draw_pdf_import", uno::Sequence<uno::Any>()), uno::UNO_QUERY);
+    uno::Reference<document::XImporter> xImporter(xFilter, uno::UNO_QUERY);
+    xImporter->setTargetDocument(xComponent);
+
+    uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(rStream));
+    uno::Sequence<beans::PropertyValue> aImportArguments =
+    {
+        // XmlFilterAdaptor::importImpl() mandates URL, even if it's empty.
+        comphelper::makePropertyValue("URL", OUString()),
+        comphelper::makePropertyValue("InputStream", xStream),
+    };
+
+    if (xFilter->filter(aImportArguments))
+        return xComponent;
+    else
+    {
+        xComponent->dispose();
+        return uno::Reference<lang::XComponent>();
+    }
+}
+
+}
+
+VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic)
+{
+    uno::Reference<lang::XComponent> xComponent = importIntoDraw(rStream);
+    if (!xComponent.is())
+        return false;
+    comphelper::ScopeGuard aGuard([&xComponent]()
+    {
+        xComponent->dispose();
+    });
+
+    // Get the preview of the first page.
+    uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(xComponent, uno::UNO_QUERY);
+    uno::Reference<drawing::XDrawPages> xDrawPages = xDrawPagesSupplier->getDrawPages();
+    if (xDrawPages->getCount() <= 0)
+        return false;
+
+    uno::Reference<beans::XPropertySet> xFirstPage(xDrawPages->getByIndex(0), uno::UNO_QUERY);
+    uno::Sequence<sal_Int8> aSequence;
+    if (!(xFirstPage->getPropertyValue("Preview") >>= aSequence))
+        return false;
+
+    if (!aSequence.hasElements())
+        return false;
+
+    // Convert it into a GDIMetaFile.
+    SvMemoryStream aPreviewStream(aSequence.getLength());
+    aPreviewStream.WriteBytes(aSequence.getArray(), aSequence.getLength());
+    aPreviewStream.Seek(0);
+    GDIMetaFile aMtf;
+    if (!ConvertWMFToGDIMetaFile(aPreviewStream, aMtf))
+        return false;
+
+    rGraphic = aMtf;
+
+    return true;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/filter/ipdf/pdfread.hxx b/vcl/source/filter/ipdf/pdfread.hxx
new file mode 100644
index 0000000..02dc845
--- /dev/null
+++ b/vcl/source/filter/ipdf/pdfread.hxx
@@ -0,0 +1,21 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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/.
+ */
+
+#ifndef INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
+#define INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
+
+#include <tools/stream.hxx>
+#include <vcl/graph.hxx>
+
+/// Imports a PDF stream into rGraphic as a GDIMetaFile.
+VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic);
+
+#endif // INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list