diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-05-29 23:52:50 +0200 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-07-30 15:07:11 +0200 |
commit | 97cdbd4f3719f10942865db01efb7e9b62db6bfa (patch) | |
tree | b32e0f9274b1b8fb5e608d5ec571bfd5c84450ed | |
parent | e5133f196d97212440219d3b03e5f7c8ffe9f9c6 (diff) |
vcl: add search start position support for VectorGraphicSearch
By default we start at the begin of the page, but with this change
make it possible to start at the end. This makes it possible to
search in the backwards direction (set the start position at to
the end and search with "previous").
Change-Id: I78fb1461b86bf9eab2f91c3b9a81cbb5c6557332
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95382
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit 83d27791fed75941c75d3cc571c3d5cf27d14e8c)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95930
Tested-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit d7e75f660b3cdfa43ffa572f7a150263b7ed27e8)
-rw-r--r-- | include/vcl/VectorGraphicSearch.hxx | 12 | ||||
-rw-r--r-- | vcl/qa/cppunit/VectorGraphicSearchTest.cxx | 79 | ||||
-rw-r--r-- | vcl/source/graphic/VectorGraphicSearch.cxx | 28 |
3 files changed, 90 insertions, 29 deletions
diff --git a/include/vcl/VectorGraphicSearch.hxx b/include/vcl/VectorGraphicSearch.hxx index a00c212ad61c..b67c63a844d8 100644 --- a/include/vcl/VectorGraphicSearch.hxx +++ b/include/vcl/VectorGraphicSearch.hxx @@ -21,6 +21,12 @@ class SearchContext; +enum class SearchStartPosition +{ + Begin, + End +}; + class VCL_DLLPUBLIC VectorGraphicSearch final { private: @@ -29,12 +35,14 @@ private: Graphic maGraphic; std::unique_ptr<SearchContext> mpSearchContext; - bool searchPDF(std::shared_ptr<VectorGraphicData> const& rData, OUString const& rSearchString); + bool searchPDF(std::shared_ptr<VectorGraphicData> const& rData, OUString const& rSearchString, + SearchStartPosition eStartPosition); public: VectorGraphicSearch(Graphic const& rGraphic); ~VectorGraphicSearch(); - bool search(OUString const& rSearchString); + bool search(OUString const& rSearchString, + SearchStartPosition eStartPosition = SearchStartPosition::Begin); basegfx::B2DSize pageSize(); bool next(); bool previous(); diff --git a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx index 7962c23f4e8f..5f65b4ba7e3d 100644 --- a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx +++ b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx @@ -93,32 +93,71 @@ void VectorGraphicSearchTest::testNextPrevious() Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream); aGraphic.makeAvailable(); - VectorGraphicSearch aSearch(aGraphic); - CPPUNIT_ASSERT_EQUAL(true, aSearch.search("lazy")); + { // Start from the beginning of the page + VectorGraphicSearch aSearch(aGraphic); + CPPUNIT_ASSERT_EQUAL(true, aSearch.search("lazy")); - // next - first match found - CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); - CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + // no previous - we are at the begin + CPPUNIT_ASSERT_EQUAL(false, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(0, aSearch.index()); // nothing was yet found, so it is 0 - // next - second match found - CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); - CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + // next - first position found + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); - // next - not found, index unchanged - CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); - CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + // next - second position found + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); - // previous - first match - CPPUNIT_ASSERT_EQUAL(true, aSearch.previous()); - CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + // next - not found, index unchanged + CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); - // previous - not found, index unchanged - CPPUNIT_ASSERT_EQUAL(false, aSearch.previous()); - CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + // previous - first position + CPPUNIT_ASSERT_EQUAL(true, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); - // next - second match found - CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); - CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + // previous - not found, index unchanged + CPPUNIT_ASSERT_EQUAL(false, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + + // next - second position found + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + } + + { // Start from the end of the page + VectorGraphicSearch aSearch(aGraphic); + CPPUNIT_ASSERT_EQUAL(true, aSearch.search("lazy", SearchStartPosition::End)); + + // no next - we are at the end + CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(0, aSearch.index()); // nothing was yet found, so it is 0 + + // previous - second position found + CPPUNIT_ASSERT_EQUAL(true, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + + // previous - first position found + CPPUNIT_ASSERT_EQUAL(true, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + + // previous - not found, index unchanged + CPPUNIT_ASSERT_EQUAL(false, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + + // next - second position + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + + // next - not found, index unchanged + CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(817, aSearch.index()); + + // previous - first match found + CPPUNIT_ASSERT_EQUAL(true, aSearch.previous()); + CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + } } CPPUNIT_TEST_SUITE_REGISTRATION(VectorGraphicSearchTest); diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 8b73cab71340..42d98a981ef9 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -42,14 +42,17 @@ public: FPDF_PAGE mpPage; FPDF_TEXTPAGE mpTextPage; OUString maSearchString; + SearchStartPosition meStartPosition; FPDF_SCHHANDLE mpSearchHandle; - SearchContext(FPDF_DOCUMENT pPdfDocument, sal_Int32 nPageIndex, OUString const& rSearchString) + SearchContext(FPDF_DOCUMENT pPdfDocument, sal_Int32 nPageIndex, OUString const& rSearchString, + SearchStartPosition eStartPosition) : mpPdfDocument(pPdfDocument) , mnPageIndex(nPageIndex) , mpPage(nullptr) , mpTextPage(nullptr) , maSearchString(rSearchString) + , meStartPosition(eStartPosition) , mpSearchHandle(nullptr) { } @@ -92,7 +95,17 @@ public: return false; FPDF_WIDESTRING pString = reinterpret_cast<FPDF_WIDESTRING>(maSearchString.getStr()); - mpSearchHandle = FPDFText_FindStart(mpTextPage, pString, 0, 0); + + // Index where to start to search. -1 => at the end + int nStartIndex = meStartPosition == SearchStartPosition::End ? -1 : 0; + + // FPDF_MATCHCASE, FPDF_MATCHWHOLEWORD, FPDF_CONSECUTIVE + // FPDF_MATCHCASE - If not set, it will not match case by default. + // FPDF_MATCHWHOLEWORD - If not set, it will not match the whole word by default. + // FPDF_CONSECUTIVE - If not set, it will skip past the current match to look for the next match. + int nSearchFlags = 0; + + mpSearchHandle = FPDFText_FindStart(mpTextPage, pString, nSearchFlags, nStartIndex); return mpSearchHandle != nullptr; } @@ -183,19 +196,20 @@ VectorGraphicSearch::~VectorGraphicSearch() FPDF_DestroyLibrary(); } -bool VectorGraphicSearch::search(OUString const& rSearchString) +bool VectorGraphicSearch::search(OUString const& rSearchString, SearchStartPosition eStartPosition) { auto pData = maGraphic.getVectorGraphicData(); if (pData && pData->getVectorGraphicDataType() == VectorGraphicDataType::Pdf) { - return searchPDF(pData, rSearchString); + return searchPDF(pData, rSearchString, eStartPosition); } return false; } bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& rData, - OUString const& rSearchString) + OUString const& rSearchString, + SearchStartPosition eStartPosition) { if (rSearchString.isEmpty()) return false; @@ -231,8 +245,8 @@ bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& rD sal_Int32 nPageIndex = std::max(rData->getPageIndex(), sal_Int32(0)); - mpSearchContext.reset( - new SearchContext(mpImplementation->mpPdfDocument, nPageIndex, rSearchString)); + mpSearchContext.reset(new SearchContext(mpImplementation->mpPdfDocument, nPageIndex, + rSearchString, eStartPosition)); return mpSearchContext->initialize(); } |