summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2018-06-09 13:09:35 -0400
committerAshod Nakashian <ashod.nakashian@collabora.co.uk>2018-06-17 22:37:56 -0400
commitd271bb61e216556887dae60d7b7421c9cf236a1b (patch)
tree6b515ae5b4a30bc291c76ef862befe4b9fb29d3d
parentf8c68132d9f48b5e935a475c5f6de3aaf16da390 (diff)
vcl: sd: import PDF as unloaded images
Change-Id: Ibf9afeab2cb580a245af666b906310de1c9b351f
-rw-r--r--include/vcl/pdfread.hxx34
-rw-r--r--sd/source/filter/pdf/sdpdffilter.cxx34
-rw-r--r--vcl/source/filter/ipdf/pdfread.cxx74
3 files changed, 115 insertions, 27 deletions
diff --git a/include/vcl/pdfread.hxx b/include/vcl/pdfread.hxx
index 5d79c6d38146..5e482850d58d 100644
--- a/include/vcl/pdfread.hxx
+++ b/include/vcl/pdfread.hxx
@@ -10,16 +10,33 @@
#ifndef INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
#define INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
+#include <vector>
#include <tools/stream.hxx>
#include <vcl/graph.hxx>
+#include <tools/gen.hxx>
+
+namespace com
+{
+namespace sun
+{
+namespace star
+{
+namespace uno
+{
+template <typename> class Sequence;
+}
+}
+}
+}
+class Bitmap;
+class Graphic;
namespace vcl
{
/// Imports a PDF stream into rGraphic as a GDIMetaFile.
-VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Bitmap &rBitmap,
- size_t nPageIndex,
- css::uno::Sequence<sal_Int8> &rPdfFata,
+VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Bitmap& rBitmap, size_t nPageIndex,
+ css::uno::Sequence<sal_Int8>& rPdfData,
sal_uInt64 nPos = STREAM_SEEK_TO_BEGIN,
sal_uInt64 nSize = STREAM_SEEK_TO_END,
const double fResolutionDPI = 96.);
@@ -27,10 +44,17 @@ VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Bitmap &rBitmap,
VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic,
const double fResolutionDPI = 96.);
-VCL_DLLPUBLIC size_t ImportPDF(const OUString& rURL,
- std::vector<Bitmap>& rBitmaps,
+VCL_DLLPUBLIC size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps,
css::uno::Sequence<sal_Int8>& rPdfData,
const double fResolutionDPI = 96.);
+
+/// Import PDF as Graphic images (1 per page), all unloaded.
+/// Since Graphic is unloaded, we need to return the page size (in pixels) separately.
+/// Does not set rPdfData if no conversion is done.
+/// Returns the number of pages read.
+VCL_DLLPUBLIC size_t ImportPDFUnloaded(const OUString& rURL,
+ std::vector<std::pair<Graphic, Size>>& rGraphics,
+ const double fResolutionDPI = 96.);
}
#endif // INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX
diff --git a/sd/source/filter/pdf/sdpdffilter.cxx b/sd/source/filter/pdf/sdpdffilter.cxx
index bb921fdbfb0b..62dbc3964f35 100644
--- a/sd/source/filter/pdf/sdpdffilter.cxx
+++ b/sd/source/filter/pdf/sdpdffilter.cxx
@@ -102,37 +102,29 @@ bool SdPdfFilter::Import()
// Rendering resolution.
const double dResolutionDPI = 96.;
- uno::Sequence<sal_Int8> aPdfData;
- std::vector<Bitmap> aBitmaps;
- if (vcl::ImportPDF(aFileName, aBitmaps, aPdfData, dResolutionDPI) == 0)
+ std::vector<std::pair<Graphic, Size>> aGraphics;
+ if (vcl::ImportPDFUnloaded(aFileName, aGraphics, dResolutionDPI) == 0)
return false;
- // Prepare the link with the PDF stream.
- const size_t nGraphicContentSize = aPdfData.getLength();
- std::unique_ptr<sal_uInt8[]> pGraphicContent(new sal_uInt8[nGraphicContentSize]);
- memcpy(pGraphicContent.get(), aPdfData.get(), nGraphicContentSize);
- std::shared_ptr<GfxLink> pGfxLink(std::make_shared<GfxLink>(
- std::move(pGraphicContent), nGraphicContentSize, GfxLinkType::NativePdf));
- auto pPdfData = std::make_shared<uno::Sequence<sal_Int8>>(aPdfData);
-
+ // Add as many pages as we need up-front.
mrDocument.CreateFirstPages();
- for (size_t i = 0; i < aBitmaps.size() - 1; ++i)
+ for (int i = 0; i < aGraphics.size() - 1; ++i)
{
mrDocument.DuplicatePage(0);
}
- size_t nPageNumber = 0;
- for (Bitmap& aBitmap : aBitmaps)
+ for (std::pair<Graphic, Size>& aPair : aGraphics)
{
- // Create the Graphic and link the original PDF stream.
- Graphic aGraphic(aBitmap);
- aGraphic.setPdfData(pPdfData);
- aGraphic.setPageNumber(nPageNumber);
- aGraphic.SetGfxLink(pGfxLink);
+ const Graphic& aGraphic = aPair.first;
+ const Size& aSize = aPair.second;
+
+ const sal_Int32 nPageNumber = aGraphic.getPageNumber();
+ if (nPageNumber < 0 || nPageNumber >= aGraphics.size())
+ continue; // Page is out of range
// Create the page and insert the Graphic.
- SdPage* pPage = mrDocument.GetSdPage(nPageNumber++, PageKind::Standard);
- Size aGrfSize(OutputDevice::LogicToLogic(aGraphic.GetPrefSize(), aGraphic.GetPrefMapMode(),
+ SdPage* pPage = mrDocument.GetSdPage(nPageNumber, PageKind::Standard);
+ Size aGrfSize(OutputDevice::LogicToLogic(aSize, aGraphic.GetPrefMapMode(),
MapMode(MapUnit::Map100thMM)));
// Resize to original size based on 72 dpi to preserve page size.
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
index 016616e69dfc..145653abcfbc 100644
--- a/vcl/source/filter/ipdf/pdfread.cxx
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -23,8 +23,10 @@
#endif
#endif
-#include <vcl/bitmapaccess.hxx>
+#include <impgraph.hxx>
+
#include <vcl/graph.hxx>
+#include <vcl/bitmapaccess.hxx>
#include <unotools/ucbstreamhelper.hxx>
using namespace com::sun::star;
@@ -297,6 +299,76 @@ size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps,
return rBitmaps.size();
}
+
+size_t ImportPDFUnloaded(const OUString& rURL, std::vector<std::pair<Graphic, Size>>& rGraphics,
+ const double fResolutionDPI)
+{
+ std::unique_ptr<SvStream> xStream(
+ ::utl::UcbStreamHelper::CreateStream(rURL, StreamMode::READ | StreamMode::SHARE_DENYNONE));
+
+ // Save the original PDF stream for later use.
+ SvMemoryStream aMemoryStream;
+ if (!getCompatibleStream(*xStream, aMemoryStream, STREAM_SEEK_TO_BEGIN, STREAM_SEEK_TO_END))
+ return 0;
+
+ // Copy into PdfData
+ uno::Sequence<sal_Int8> aPdfData;
+ aMemoryStream.Seek(STREAM_SEEK_TO_END);
+ aPdfData = css::uno::Sequence<sal_Int8>(aMemoryStream.Tell());
+ aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+ aMemoryStream.ReadBytes(aPdfData.getArray(), aPdfData.getLength());
+
+ // Prepare the link with the PDF stream.
+ const size_t nGraphicContentSize = aPdfData.getLength();
+ std::unique_ptr<sal_uInt8[]> pGraphicContent(new sal_uInt8[nGraphicContentSize]);
+ memcpy(pGraphicContent.get(), aPdfData.get(), nGraphicContentSize);
+ std::shared_ptr<GfxLink> pGfxLink(std::make_shared<GfxLink>(
+ std::move(pGraphicContent), nGraphicContentSize, GfxLinkType::NativePdf));
+ auto pPdfData = std::make_shared<uno::Sequence<sal_Int8>>(aPdfData);
+
+ FPDF_LIBRARY_CONFIG aConfig;
+ aConfig.version = 2;
+ aConfig.m_pUserFontPaths = nullptr;
+ aConfig.m_pIsolate = nullptr;
+ aConfig.m_v8EmbedderSlot = 0;
+ FPDF_InitLibraryWithConfig(&aConfig);
+
+ // Load the buffer using pdfium.
+ FPDF_DOCUMENT pPdfDocument
+ = FPDF_LoadMemDocument(aPdfData.getArray(), aPdfData.getLength(), /*password=*/nullptr);
+ if (!pPdfDocument)
+ return 0;
+
+ const int nPageCount = FPDF_GetPageCount(pPdfDocument);
+ if (nPageCount <= 0)
+ return 0;
+
+ for (size_t nPageIndex = 0; nPageIndex < static_cast<size_t>(nPageCount); ++nPageIndex)
+ {
+ double fPageWidth = 0;
+ double fPageHeight = 0;
+ if (FPDF_GetPageSizeByIndex(pPdfDocument, nPageIndex, &fPageWidth, &fPageHeight) == 0)
+ continue;
+
+ // Returned unit is points, convert that to pixel.
+ const size_t nPageWidth = pointToPixel(fPageWidth, fResolutionDPI);
+ const size_t nPageHeight = pointToPixel(fPageHeight, fResolutionDPI);
+
+ // Create the Graphic and link the original PDF stream.
+ Graphic aGraphic;
+ aGraphic.setPdfData(pPdfData); // TODO: Skip if unchanged.
+ aGraphic.setPageNumber(nPageIndex);
+ aGraphic.SetGfxLink(pGfxLink);
+ aGraphic.ImplGetImpGraphic()->ImplSetPrepared(); // Prepared but not loaded.
+
+ rGraphics.emplace_back(std::move(aGraphic), Size(nPageWidth, nPageHeight));
+ }
+
+ FPDF_CloseDocument(pPdfDocument);
+ FPDF_DestroyLibrary();
+
+ return rGraphics.size();
+}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */