diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2018-06-09 13:09:35 -0400 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2019-04-03 17:54:00 +0200 |
commit | 5a1f367b5573293cf3b2e8dbdffc7de22e99c058 (patch) | |
tree | cc43148087a3c2317905b4b0aa4c493cc024bd07 | |
parent | Fix linking error in the !HAVE_FEATURE_PDFIUM case (diff) | |
download | core-5a1f367b5573293cf3b2e8dbdffc7de22e99c058.tar.gz core-5a1f367b5573293cf3b2e8dbdffc7de22e99c058.zip |
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>
-rw-r--r-- | include/vcl/pdfread.hxx | 25 | ||||
-rw-r--r-- | sd/source/filter/pdf/sdpdffilter.cxx | 35 | ||||
-rw-r--r-- | vcl/source/filter/ipdf/pdfread.cxx | 73 | ||||
-rw-r--r-- | vcl/source/gdi/impgraph.cxx | 7 |
4 files changed, 117 insertions, 23 deletions
diff --git a/include/vcl/pdfread.hxx b/include/vcl/pdfread.hxx index c491f50ef34b..e579f435261e 100644 --- a/include/vcl/pdfread.hxx +++ b/include/vcl/pdfread.hxx @@ -13,6 +13,23 @@ #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 { @@ -34,6 +51,14 @@ VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic, 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 95daaeb479f1..3ef639853184 100644 --- a/sd/source/filter/pdf/sdpdffilter.cxx +++ b/sd/source/filter/pdf/sdpdffilter.cxx @@ -102,38 +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.SetSharedLink(pGfxLink); - aGraphic.setOriginURL(aFileName); + 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 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() |