summaryrefslogtreecommitdiff
path: root/vcl
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2020-05-29 23:52:50 +0200
committerTomaž Vajngerl <quikee@gmail.com>2020-06-03 13:26:40 +0200
commit83d27791fed75941c75d3cc571c3d5cf27d14e8c (patch)
tree4fa75b80e322791796af560d110deebdd545d688 /vcl
parent997a504f33bbd43c02a96a2a593a03e58bd3b72c (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>
Diffstat (limited to 'vcl')
-rw-r--r--vcl/qa/cppunit/VectorGraphicSearchTest.cxx79
-rw-r--r--vcl/source/graphic/VectorGraphicSearch.cxx28
2 files changed, 80 insertions, 27 deletions
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 dd37c9fc98cf..76e8c59404a9 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)
{
}
@@ -91,7 +94,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;
}
@@ -182,19 +195,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;
@@ -230,8 +244,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();
}