include/vcl/VectorGraphicSearch.hxx | 3 include/vcl/filter/PDFiumLibrary.hxx | 43 +++++++++++++ svx/source/inc/svdpdf.hxx | 4 + svx/source/svdraw/svdpdf.cxx | 14 ---- vcl/Library_vcl.mk | 1 vcl/qa/cppunit/pdfexport/pdfexport.cxx | 20 +----- vcl/source/filter/ipdf/pdfread.cxx | 26 +------- vcl/source/graphic/VectorGraphicSearch.cxx | 92 ++++++++++++++++++++--------- vcl/source/pdf/PDFiumLibrary.cxx | 36 +++++++++++ 9 files changed, 161 insertions(+), 78 deletions(-)
New commits: commit 292f7575ca2b0719ed62dd4cb4ce01bd837fe9b4 Author: Tomaž Vajngerl <[email protected]> AuthorDate: Sun May 31 12:13:59 2020 +0200 Commit: Tomaž Vajngerl <[email protected]> CommitDate: Sun May 31 12:13:59 2020 +0200 vcl: use HAVE_FEATURE_PDFIUM in VectorGraphicSearch impl. Change-Id: Id6c30e8f1c5bdb0481b0c7d4680554e3e8caa323 diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 0e50a045e911..db17df2539ec 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -10,6 +10,10 @@ #include <vcl/VectorGraphicSearch.hxx> +#include <config_features.h> + +#if HAVE_FEATURE_PDFIUM + #include <vcl/filter/PDFiumLibrary.hxx> #include <sal/config.h> @@ -286,4 +290,41 @@ std::vector<basegfx::B2DRectangle> VectorGraphicSearch::getTextRectangles() return std::vector<basegfx::B2DRectangle>(); } +#else // !HAVE_FEATURE_PDFIUM + +VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic) + : maGraphic(rGraphic) +{ +} + +VectorGraphicSearch::~VectorGraphicSearch() {} + +bool VectorGraphicSearch::search(OUString const& /*rSearchString*/, + SearchStartPosition /*eStartPosition*/) +{ + return false; +} + +bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& /*rData*/, + OUString const& /*rSearchString*/, + SearchStartPosition /*eStartPosition*/) +{ + return false; +} + +basegfx::B2DSize VectorGraphicSearch::pageSize() { return basegfx::B2DSize(); } + +bool VectorGraphicSearch::next() { return false; } + +bool VectorGraphicSearch::previous() { return false; } + +int VectorGraphicSearch::index() { return -1; } + +std::vector<basegfx::B2DRectangle> VectorGraphicSearch::getTextRectangles() +{ + return std::vector<basegfx::B2DRectangle>(); +} + +#endif // HAVE_FEATURE_PDFIUM + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ commit e36364f7d21c99e502442b8ae2400c4a3a7b7fac Author: Tomaž Vajngerl <[email protected]> AuthorDate: Sun May 31 12:02:39 2020 +0200 Commit: Tomaž Vajngerl <[email protected]> CommitDate: Sun May 31 12:02:39 2020 +0200 vcl: VectorGraphicSearch - move SearchContext into Implementation Change-Id: I3bbf085fd8b8b66a56e364168c1e70b4ce986467 diff --git a/include/vcl/VectorGraphicSearch.hxx b/include/vcl/VectorGraphicSearch.hxx index b67c63a844d8..2dc8cca3b76a 100644 --- a/include/vcl/VectorGraphicSearch.hxx +++ b/include/vcl/VectorGraphicSearch.hxx @@ -19,8 +19,6 @@ #include <memory> -class SearchContext; - enum class SearchStartPosition { Begin, @@ -33,7 +31,6 @@ private: class Implementation; std::unique_ptr<Implementation> mpImplementation; Graphic maGraphic; - std::unique_ptr<SearchContext> mpSearchContext; bool searchPDF(std::shared_ptr<VectorGraphicData> const& rData, OUString const& rSearchString, SearchStartPosition eStartPosition); diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 9a1c966d9d69..0e50a045e911 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -17,12 +17,16 @@ #include <fpdf_doc.h> #include <fpdf_text.h> +class SearchContext; + class VectorGraphicSearch::Implementation { public: std::shared_ptr<vcl::pdf::PDFium> mpPDFium; FPDF_DOCUMENT mpPdfDocument; + std::unique_ptr<SearchContext> mpSearchContext; + Implementation() : mpPDFium(vcl::pdf::PDFiumLibrary::get()) , mpPdfDocument(nullptr) @@ -31,6 +35,8 @@ public: ~Implementation() { + mpSearchContext.reset(); + if (mpPdfDocument) FPDF_CloseDocument(mpPdfDocument); } @@ -186,11 +192,7 @@ VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic) { } -VectorGraphicSearch::~VectorGraphicSearch() -{ - mpSearchContext.reset(); - mpImplementation.reset(); -} +VectorGraphicSearch::~VectorGraphicSearch() { mpImplementation.reset(); } bool VectorGraphicSearch::search(OUString const& rSearchString, SearchStartPosition eStartPosition) { @@ -241,45 +243,45 @@ bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& rD sal_Int32 nPageIndex = std::max(rData->getPageIndex(), 0); - mpSearchContext.reset(new SearchContext(mpImplementation->mpPdfDocument, nPageIndex, - rSearchString, eStartPosition)); + mpImplementation->mpSearchContext.reset(new SearchContext( + mpImplementation->mpPdfDocument, nPageIndex, rSearchString, eStartPosition)); - return mpSearchContext->initialize(); + return mpImplementation->mpSearchContext->initialize(); } basegfx::B2DSize VectorGraphicSearch::pageSize() { basegfx::B2DSize aSize; - if (mpSearchContext) - aSize = mpSearchContext->getPageSize(); + if (mpImplementation->mpSearchContext) + aSize = mpImplementation->mpSearchContext->getPageSize(); return aSize; } bool VectorGraphicSearch::next() { - if (mpSearchContext) - return mpSearchContext->next(); + if (mpImplementation->mpSearchContext) + return mpImplementation->mpSearchContext->next(); return false; } bool VectorGraphicSearch::previous() { - if (mpSearchContext) - return mpSearchContext->previous(); + if (mpImplementation->mpSearchContext) + return mpImplementation->mpSearchContext->previous(); return false; } int VectorGraphicSearch::index() { - if (mpSearchContext) - return mpSearchContext->index(); + if (mpImplementation->mpSearchContext) + return mpImplementation->mpSearchContext->index(); return -1; } std::vector<basegfx::B2DRectangle> VectorGraphicSearch::getTextRectangles() { - if (mpSearchContext) - return mpSearchContext->getTextRectangles(); + if (mpImplementation->mpSearchContext) + return mpImplementation->mpSearchContext->getTextRectangles(); return std::vector<basegfx::B2DRectangle>(); } commit 9b6c3df1b71ecde00c63a1f58ed6bfdc4fd828f3 Author: Tomaž Vajngerl <[email protected]> AuthorDate: Sun May 31 11:50:20 2020 +0200 Commit: Tomaž Vajngerl <[email protected]> CommitDate: Sun May 31 11:50:20 2020 +0200 pdfium: only init pdfium library one and destroy on LO exit With more and more usage of PDFium, it is hard to keep track of the life-time of the PDFium library, so it can happen that a FPDF_DestroyLibrary happens when we still have another instance where PDFium is still use. The result of this is a crash. To prevent this, just initialize the library once and delete, when on LO exit. This can be improved in the future to only keep the library active when in actual use. Change-Id: I5c7e5de7f8b97d10efb394c67c7a61b976c8d57c diff --git a/include/vcl/filter/PDFiumLibrary.hxx b/include/vcl/filter/PDFiumLibrary.hxx new file mode 100644 index 000000000000..bc7912c17e81 --- /dev/null +++ b/include/vcl/filter/PDFiumLibrary.hxx @@ -0,0 +1,43 @@ +/* -*- 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/. + * + */ + +#pragma once + +#include <config_features.h> + +#if HAVE_FEATURE_PDFIUM + +#include <memory> +#include <rtl/instance.hxx> +#include <vcl/dllapi.h> + +namespace vcl::pdf +{ +class VCL_DLLPUBLIC PDFium final +{ +private: + PDFium(const PDFium&) = delete; + PDFium& operator=(const PDFium&) = delete; + +public: + PDFium(); + ~PDFium(); +}; + +struct PDFiumLibrary : public rtl::StaticWithInit<std::shared_ptr<PDFium>, PDFiumLibrary> +{ + std::shared_ptr<PDFium> operator()() { return std::make_shared<PDFium>(); } +}; + +} // namespace vcl::pdf + +#endif // HAVE_FEATURE_PDFIUM + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svx/source/inc/svdpdf.hxx b/svx/source/inc/svdpdf.hxx index 9b48eb1d30c5..1f02b2151594 100644 --- a/svx/source/inc/svdpdf.hxx +++ b/svx/source/inc/svdpdf.hxx @@ -42,6 +42,8 @@ #include <postwin.h> #include <fpdfview.h> +#include <vcl/filter/PDFiumLibrary.hxx> + // Forward Declarations class SfxItemSet; @@ -100,6 +102,8 @@ class SVXCORE_DLLPUBLIC ImpSdrPdfImport final tools::Rectangle PointsToLogic(double left, double right, double top, double bottom) const; Point PointsToLogic(double x, double y) const; + std::shared_ptr<vcl::pdf::PDFium> mpPDFium; + // check for clip and evtl. fill maClip void checkClip(); bool isClip() const; diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx index 5cd0c0765a29..188651be386c 100644 --- a/svx/source/svdraw/svdpdf.cxx +++ b/svx/source/svdraw/svdpdf.cxx @@ -116,6 +116,7 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools: , mnPageCount(0) , mdPageWidthPts(0) , mdPageHeightPts(0) + , mpPDFium(vcl::pdf::PDFiumLibrary::get()) { mpVD->EnableOutput(false); mpVD->SetLineColor(); @@ -129,13 +130,6 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools: svl::Items<EE_ITEMS_START, EE_ITEMS_END>{}); checkClip(); - 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. auto const& rVectorGraphicData = rGraphic.getVectorGraphicData(); mpPdfDocument = FPDF_LoadMemDocument( @@ -170,11 +164,7 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools: mnPageCount = FPDF_GetPageCount(mpPdfDocument); } -ImpSdrPdfImport::~ImpSdrPdfImport() -{ - FPDF_CloseDocument(mpPdfDocument); - FPDF_DestroyLibrary(); -} +ImpSdrPdfImport::~ImpSdrPdfImport() { FPDF_CloseDocument(mpPdfDocument); } void ImpSdrPdfImport::DoObjects(SvdProgressInfo* pProgrInfo, sal_uInt32* pActionsToReport, int nPageIndex) diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index b1422157df0e..50d6513be8b0 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -319,6 +319,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/pdf/ResourceDict \ vcl/source/pdf/Matrix3 \ vcl/source/pdf/XmpMetadata \ + vcl/source/pdf/PDFiumLibrary \ vcl/source/graphic/GraphicID \ vcl/source/graphic/GraphicLoader \ vcl/source/graphic/GraphicObject \ diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx b/vcl/qa/cppunit/pdfexport/pdfexport.cxx index a171a6791292..fe402b4ed8b8 100644 --- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx +++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx @@ -43,6 +43,8 @@ #include <basegfx/matrix/b2dhommatrix.hxx> #include <unotools/streamwrap.hxx> +#include <vcl/filter/PDFiumLibrary.hxx> + using namespace ::com::sun::star; static std::ostream& operator<<(std::ostream& rStrm, const Color& rColor) @@ -87,6 +89,7 @@ class PdfExportTest : public test::BootstrapFixture, public unotest::MacrosTest SvMemoryStream maMemory; // Export the document as PDF, then parse it with PDFium. DocumentHolder exportAndParse(const OUString& rURL, const utl::MediaDescriptor& rDescriptor); + std::shared_ptr<vcl::pdf::PDFium> mpPDFium; public: PdfExportTest(); @@ -225,18 +228,11 @@ void PdfExportTest::setUp() mxDesktop.set(frame::Desktop::create(mxComponentContext)); - FPDF_LIBRARY_CONFIG config; - config.version = 2; - config.m_pUserFontPaths = nullptr; - config.m_pIsolate = nullptr; - config.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&config); + mpPDFium = vcl::pdf::PDFiumLibrary::get(); } void PdfExportTest::tearDown() { - FPDF_DestroyLibrary(); - if (mxComponent.is()) mxComponent->dispose(); @@ -2061,14 +2057,6 @@ void PdfExportTest::testPdfImageResourceInlineXObjectRef() aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export"); xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList()); - // Init pdfium, vcl::ImportPDF() calls FPDF_DestroyLibrary after our setUp(). - FPDF_LIBRARY_CONFIG config; - config.version = 2; - config.m_pUserFontPaths = nullptr; - config.m_pIsolate = nullptr; - config.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&config); - // Parse the export result. SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ); maMemory.WriteStream(aFile); diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx index 1df96ad46e72..2a7e7ce04198 100644 --- a/vcl/source/filter/ipdf/pdfread.cxx +++ b/vcl/source/filter/ipdf/pdfread.cxx @@ -21,6 +21,8 @@ #include <bitmapwriteaccess.hxx> #include <unotools/ucbstreamhelper.hxx> +#include <vcl/filter/PDFiumLibrary.hxx> + using namespace com::sun::star; namespace @@ -82,12 +84,7 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream) else { // Downconvert to PDF-1.6. - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); // Read input into a buffer. SvMemoryStream aInBuffer; @@ -108,7 +105,6 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream) return false; FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); aWriter.m_aStream.Seek(STREAM_SEEK_TO_BEGIN); rOutStream.WriteStream(aWriter.m_aStream); @@ -152,12 +148,7 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<Bitmap>& rBi const size_t nFirstPage, int nPages, const double fResolutionDPI) { #if HAVE_FEATURE_PDFIUM - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); // Load the buffer using pdfium. FPDF_DOCUMENT pPdfDocument = FPDF_LoadMemDocument(pBuffer, nSize, /*password=*/nullptr); @@ -207,7 +198,6 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<Bitmap>& rBi } FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); return rBitmaps.size(); #else @@ -254,12 +244,7 @@ size_t ImportPDFUnloaded(const OUString& rURL, std::vector<std::pair<Graphic, Si auto pGfxLink = std::make_shared<GfxLink>(std::move(pGraphicContent), nGraphicContentSize, GfxLinkType::NativePdf); - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); // Load the buffer using pdfium. FPDF_DOCUMENT pPdfDocument @@ -298,7 +283,6 @@ size_t ImportPDFUnloaded(const OUString& rURL, std::vector<std::pair<Graphic, Si } FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); return rGraphics.size(); #else diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 2fa0d536442d..9a1c966d9d69 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -10,6 +10,8 @@ #include <vcl/VectorGraphicSearch.hxx> +#include <vcl/filter/PDFiumLibrary.hxx> + #include <sal/config.h> #include <fpdf_doc.h> @@ -18,10 +20,12 @@ class VectorGraphicSearch::Implementation { public: + std::shared_ptr<vcl::pdf::PDFium> mpPDFium; FPDF_DOCUMENT mpPdfDocument; Implementation() - : mpPdfDocument(nullptr) + : mpPDFium(vcl::pdf::PDFiumLibrary::get()) + , mpPdfDocument(nullptr) { } @@ -180,19 +184,12 @@ VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic) : mpImplementation(std::make_unique<VectorGraphicSearch::Implementation>()) , maGraphic(rGraphic) { - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); } VectorGraphicSearch::~VectorGraphicSearch() { mpSearchContext.reset(); mpImplementation.reset(); - FPDF_DestroyLibrary(); } bool VectorGraphicSearch::search(OUString const& rSearchString, SearchStartPosition eStartPosition) diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx new file mode 100644 index 000000000000..604807524bf9 --- /dev/null +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -0,0 +1,36 @@ +/* -*- 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 <config_features.h> + +#if HAVE_FEATURE_PDFIUM + +#include <vcl/filter/PDFiumLibrary.hxx> +#include <fpdf_doc.h> + +namespace vcl::pdf +{ +PDFium::PDFium() +{ + FPDF_LIBRARY_CONFIG aConfig; + aConfig.version = 2; + aConfig.m_pUserFontPaths = nullptr; + aConfig.m_pIsolate = nullptr; + aConfig.m_v8EmbedderSlot = 0; + FPDF_InitLibraryWithConfig(&aConfig); +} + +PDFium::~PDFium() { FPDF_DestroyLibrary(); } + +} // end vcl::pdf + +#endif // HAVE_FEATURE_PDFIUM + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
