summaryrefslogtreecommitdiff
path: root/vcl
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2018-06-09 13:09:35 -0400
committerJan Holesovsky <kendy@collabora.com>2019-04-03 17:54:00 +0200
commit5a1f367b5573293cf3b2e8dbdffc7de22e99c058 (patch)
treecc43148087a3c2317905b4b0aa4c493cc024bd07 /vcl
parent81ac2da0d0325fc74fafd09bc4f3c85a75fab352 (diff)
pdfium: Import PDF with unloaded images.
Change-Id: I5e4a16ff38b9643127ce16879b35f456c13bcff8 Reviewed-on: https://gerrit.libreoffice.org/56268 Reviewed-by: Ashod Nakashian <ashnakash@gmail.com> Tested-by: Ashod Nakashian <ashnakash@gmail.com>
Diffstat (limited to 'vcl')
-rw-r--r--vcl/source/filter/ipdf/pdfread.cxx73
-rw-r--r--vcl/source/gdi/impgraph.cxx7
2 files changed, 79 insertions, 1 deletions
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
index f73dc5736fbf..324218ead522 100644
--- a/vcl/source/filter/ipdf/pdfread.cxx
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -17,8 +17,10 @@
#include <fpdf_save.h>
#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;
@@ -286,6 +288,75 @@ 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.SetSharedLink(pGfxLink);
+
+ rGraphics.emplace_back(std::move(aGraphic), Size(nPageWidth, nPageHeight));
+ }
+
+ FPDF_CloseDocument(pPdfDocument);
+ FPDF_DestroyLibrary();
+
+ return rGraphics.size();
+}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/gdi/impgraph.cxx b/vcl/source/gdi/impgraph.cxx
index 30160c3fa79c..56d1c92da6a1 100644
--- a/vcl/source/gdi/impgraph.cxx
+++ b/vcl/source/gdi/impgraph.cxx
@@ -1398,7 +1398,14 @@ void ImpGraphic::ImplSetSharedLink(const std::shared_ptr<GfxLink>& pGfxLink)
mpGfxLink = pGfxLink;
if (mpGfxLink && mpGfxLink->IsNative())
+ {
mpGfxLink->SwapOut();
+
+ // Swap out the graphic as well.
+ //FIXME: move to own function, such as SetPrepared().
+ meType = GraphicType::Bitmap;
+ ImplSwapOut();
+ }
}
GfxLink ImpGraphic::ImplGetLink()