From 4f3987e0b1a995431478769c898b5ef151745254 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 28 Jan 2021 21:04:35 +0100 Subject: pdfium: add wrapper for FPDFText_FindClose() So we can call it in the dtor, so we can't forget calling it. Change-Id: I9cea58ca53763d85a4c239fd90611ac1c398564d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110092 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- include/vcl/filter/PDFiumLibrary.hxx | 15 +++++++++++++++ vcl/source/graphic/VectorGraphicSearch.cxx | 19 +++++++++---------- vcl/source/pdf/PDFiumLibrary.cxx | 11 +++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/vcl/filter/PDFiumLibrary.hxx b/include/vcl/filter/PDFiumLibrary.hxx index 0fbcf4fe2199..418feede99b6 100644 --- a/include/vcl/filter/PDFiumLibrary.hxx +++ b/include/vcl/filter/PDFiumLibrary.hxx @@ -149,6 +149,21 @@ public: virtual bool getDrawMode(PDFFillMode& eFillMode, bool& bStroke) = 0; }; +class VCL_DLLPUBLIC PDFiumSearchHandle final +{ +private: + FPDF_SCHHANDLE mpSearchHandle; + + PDFiumSearchHandle(const PDFiumSearchHandle&) = delete; + PDFiumSearchHandle& operator=(const PDFiumSearchHandle&) = delete; + +public: + PDFiumSearchHandle(FPDF_SCHHANDLE pSearchHandle); + ~PDFiumSearchHandle(); + + FPDF_SCHHANDLE getPointer() { return mpSearchHandle; } +}; + class VCL_DLLPUBLIC PDFiumTextPage final { private: diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 083dc6cf2741..3ac33db37cc3 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -30,7 +30,7 @@ private: std::unique_ptr& mpPdfDocument; std::unique_ptr mpPage; std::unique_ptr mpTextPage; - FPDF_SCHHANDLE mpSearchHandle; + std::unique_ptr mpSearchHandle; public: sal_Int32 mnPageIndex; @@ -40,7 +40,6 @@ public: SearchContext(std::unique_ptr& pPdfDocument, sal_Int32 nPageIndex) : mpPdfDocument(pPdfDocument) - , mpSearchHandle(nullptr) , mnPageIndex(nPageIndex) , mnCurrentIndex(-1) { @@ -49,7 +48,7 @@ public: ~SearchContext() { if (mpSearchHandle) - FPDFText_FindClose(mpSearchHandle); + mpSearchHandle.reset(); if (mpTextPage) mpTextPage.reset(); if (mpPage) @@ -77,7 +76,7 @@ public: return true; if (mpSearchHandle) - FPDFText_FindClose(mpSearchHandle); + mpSearchHandle.reset(); if (mpTextPage) mpTextPage.reset(); @@ -114,15 +113,15 @@ public: if (maOptions.mbMatchWholeWord) nSearchFlags |= FPDF_MATCHWHOLEWORD; - mpSearchHandle - = FPDFText_FindStart(mpTextPage->getPointer(), pString, nSearchFlags, nStartIndex); + mpSearchHandle = std::make_unique( + FPDFText_FindStart(mpTextPage->getPointer(), pString, nSearchFlags, nStartIndex)); return mpSearchHandle != nullptr; } bool next() { - if (mpSearchHandle && FPDFText_FindNext(mpSearchHandle)) + if (mpSearchHandle && FPDFText_FindNext(mpSearchHandle->getPointer())) { mnCurrentIndex = index(); return true; @@ -132,7 +131,7 @@ public: bool previous() { - if (mpSearchHandle && FPDFText_FindPrev(mpSearchHandle)) + if (mpSearchHandle && FPDFText_FindPrev(mpSearchHandle->getPointer())) { mnCurrentIndex = index(); return true; @@ -143,14 +142,14 @@ public: int index() { if (mpSearchHandle) - return FPDFText_GetSchResultIndex(mpSearchHandle); + return FPDFText_GetSchResultIndex(mpSearchHandle->getPointer()); return -1; } int size() { if (mpSearchHandle) - return FPDFText_GetSchCount(mpSearchHandle); + return FPDFText_GetSchCount(mpSearchHandle->getPointer()); return -1; } diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx index 886549031596..fce8d4d539bd 100644 --- a/vcl/source/pdf/PDFiumLibrary.cxx +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -1100,6 +1100,17 @@ unsigned int PDFiumTextPage::getUnicode(int index) return FPDFText_GetUnicode(mpTextPage, index); } +PDFiumSearchHandle::PDFiumSearchHandle(FPDF_SCHHANDLE pSearchHandle) + : mpSearchHandle(pSearchHandle) +{ +} + +PDFiumSearchHandle::~PDFiumSearchHandle() +{ + if (mpSearchHandle) + FPDFText_FindClose(mpSearchHandle); +} + } // end vcl::pdf #endif // HAVE_FEATURE_PDFIUM -- cgit v1.2.3