diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-06-04 18:26:58 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2020-06-10 08:11:41 +0200 |
commit | 456116d97b66703e23055b83ded5d87ed85c728d (patch) | |
tree | 54c5247f4921a6c82db38229fafdab495114cf0a | |
parent | 3e20ddab50de57f8a4f915791bbb56710c4936e3 (diff) |
sd: support match case, match whole word for PDF search
THis adds support for match case and match whole word to the
VectorGraphicSearch + tests.
It uses the new options in PDF seearch in Draw/Impress.
Change-Id: I20a6382c22bf01a5a021c8bae1ff78861419c0ef
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95530
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit 112d8113388513d9c6b317e828f5d373b4a54330)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95950
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
-rw-r--r-- | include/vcl/VectorGraphicSearch.hxx | 24 | ||||
-rw-r--r-- | sd/source/ui/view/Outliner.cxx | 16 | ||||
-rw-r--r-- | vcl/qa/cppunit/VectorGraphicSearchTest.cxx | 88 | ||||
-rw-r--r-- | vcl/source/graphic/VectorGraphicSearch.cxx | 22 |
4 files changed, 135 insertions, 15 deletions
diff --git a/include/vcl/VectorGraphicSearch.hxx b/include/vcl/VectorGraphicSearch.hxx index c9faaa51f1c9..4601a1f5ac1d 100644 --- a/include/vcl/VectorGraphicSearch.hxx +++ b/include/vcl/VectorGraphicSearch.hxx @@ -25,6 +25,28 @@ enum class SearchStartPosition End }; +struct VCL_DLLPUBLIC VectorGraphicSearchOptions final +{ + SearchStartPosition meStartPosition; + bool mbMatchCase; + bool mbMatchWholeWord; + + VectorGraphicSearchOptions() + : meStartPosition(SearchStartPosition::Begin) + , mbMatchCase(false) + , mbMatchWholeWord(false) + { + } + + VectorGraphicSearchOptions(SearchStartPosition eStartPosition, bool bMatchCase, + bool bMatchWholeWord) + : meStartPosition(eStartPosition) + , mbMatchCase(bMatchCase) + , mbMatchWholeWord(bMatchWholeWord) + { + } +}; + class VCL_DLLPUBLIC VectorGraphicSearch final { private: @@ -38,7 +60,7 @@ public: VectorGraphicSearch(Graphic const& rGraphic); ~VectorGraphicSearch(); bool search(OUString const& rSearchString, - SearchStartPosition eStartPosition = SearchStartPosition::Begin); + VectorGraphicSearchOptions const& rOptions = VectorGraphicSearchOptions()); basegfx::B2DSize pageSize(); bool next(); bool previous(); diff --git a/sd/source/ui/view/Outliner.cxx b/sd/source/ui/view/Outliner.cxx index 3114196f7cf2..c01e2fe6fe73 100644 --- a/sd/source/ui/view/Outliner.cxx +++ b/sd/source/ui/view/Outliner.cxx @@ -840,8 +840,12 @@ bool SdOutliner::SearchAndReplaceOnce(std::vector<sd::SearchSelection>* pSelecti OUString const & rString = mpSearchItem->GetSearchString(); bool bBackwards = mpSearchItem->GetBackward(); - SearchStartPosition eSearchStartPosition = bBackwards ? SearchStartPosition::End : SearchStartPosition::Begin; - bool bResult = mpImpl->mpVectorGraphicSearch->search(rString, eSearchStartPosition); + VectorGraphicSearchOptions aOptions; + aOptions.meStartPosition = bBackwards ? SearchStartPosition::End : SearchStartPosition::Begin; + aOptions.mbMatchCase = mpSearchItem->GetExact(); + aOptions.mbMatchWholeWord = mpSearchItem->GetWordOnly(); + + bool bResult = mpImpl->mpVectorGraphicSearch->search(rString, aOptions); if (bResult) { @@ -1253,11 +1257,15 @@ void SdOutliner::ProvideNextTextObject() auto* pGraphicObject = static_cast<SdrGrafObj*>(mpObj); OUString const & rString = mpSearchItem->GetSearchString(); bool bBackwards = mpSearchItem->GetBackward(); - SearchStartPosition eSearchStartPosition = bBackwards ? SearchStartPosition::End : SearchStartPosition::Begin; + + VectorGraphicSearchOptions aOptions; + aOptions.meStartPosition = bBackwards ? SearchStartPosition::End : SearchStartPosition::Begin; + aOptions.mbMatchCase = mpSearchItem->GetExact(); + aOptions.mbMatchWholeWord = mpSearchItem->GetWordOnly(); mpImpl->mpVectorGraphicSearch = std::make_unique<VectorGraphicSearch>(pGraphicObject->GetGraphic()); - bool bResult = mpImpl->mpVectorGraphicSearch->search(rString, eSearchStartPosition); + bool bResult = mpImpl->mpVectorGraphicSearch->search(rString, aOptions); if (bResult) { if (bBackwards) diff --git a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx index 00febce16e71..0659e4e62dcf 100644 --- a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx +++ b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx @@ -32,11 +32,15 @@ class VectorGraphicSearchTest : public test::BootstrapFixtureBase void test(); void testNextPrevious(); void testSearchStringChange(); + void testSearchMatchWholeWord(); + void testSearchMatchCase(); CPPUNIT_TEST_SUITE(VectorGraphicSearchTest); CPPUNIT_TEST(test); CPPUNIT_TEST(testNextPrevious); CPPUNIT_TEST(testSearchStringChange); + CPPUNIT_TEST(testSearchMatchWholeWord); + CPPUNIT_TEST(testSearchMatchCase); CPPUNIT_TEST_SUITE_END(); }; @@ -134,7 +138,8 @@ void VectorGraphicSearchTest::testNextPrevious() { // Start from the end of the page VectorGraphicSearch aSearch(aGraphic); - CPPUNIT_ASSERT_EQUAL(true, aSearch.search("lazy", SearchStartPosition::End)); + CPPUNIT_ASSERT_EQUAL(true, + aSearch.search("lazy", { SearchStartPosition::End, false, false })); // no next - we are at the end CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); @@ -197,6 +202,87 @@ void VectorGraphicSearchTest::testSearchStringChange() CPPUNIT_ASSERT_EQUAL(784, aSearch.index()); } +void VectorGraphicSearchTest::testSearchMatchWholeWord() +{ + OUString aURL = getFullUrl("Pangram.pdf"); + SvFileStream aStream(aURL, StreamMode::READ); + GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter(); + Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream); + aGraphic.makeAvailable(); + + { + VectorGraphicSearch aSearch(aGraphic); + // Search, whole word disabled - "Flummoxed" - found + CPPUNIT_ASSERT_EQUAL(true, aSearch.search("Flummoxed")); + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(618, aSearch.index()); + } + { + VectorGraphicSearch aSearch(aGraphic); + // Search, whole word disabled - "Flummo" - found + CPPUNIT_ASSERT_EQUAL(true, aSearch.search("Flummo")); + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(618, aSearch.index()); + } + { + VectorGraphicSearch aSearch(aGraphic); + // Search, whole word enabled - "Flummoxed" - found + CPPUNIT_ASSERT_EQUAL( + true, aSearch.search("Flummoxed", { SearchStartPosition::Begin, false, true })); + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(618, aSearch.index()); + } + { + VectorGraphicSearch aSearch(aGraphic); + // Search, whole word enabled - "Flummo" - not found + CPPUNIT_ASSERT_EQUAL(true, + aSearch.search("Flummo", { SearchStartPosition::Begin, false, true })); + CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); + } +} + +void VectorGraphicSearchTest::testSearchMatchCase() +{ + OUString aURL = getFullUrl("Pangram.pdf"); + SvFileStream aStream(aURL, StreamMode::READ); + GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter(); + Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream); + aGraphic.makeAvailable(); + + { + VectorGraphicSearch aSearch(aGraphic); + // Search "Flummoxed" - case insensitive - found + CPPUNIT_ASSERT_EQUAL(true, aSearch.search("Flummoxed")); + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(618, aSearch.index()); + } + + { + VectorGraphicSearch aSearch(aGraphic); + // Search "FLUMMOXED" - case insensitive - found + CPPUNIT_ASSERT_EQUAL(true, aSearch.search("FLUMMOXED")); + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(618, aSearch.index()); + } + + { + VectorGraphicSearch aSearch(aGraphic); + // Search "Flummoxed" - case sensitive - found + CPPUNIT_ASSERT_EQUAL( + true, aSearch.search("Flummoxed", { SearchStartPosition::Begin, true, false })); + CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); + CPPUNIT_ASSERT_EQUAL(618, aSearch.index()); + } + + { + VectorGraphicSearch aSearch(aGraphic); + // Search to "FLUMMOXED" - case sensitive - not found + CPPUNIT_ASSERT_EQUAL( + true, aSearch.search("FLUMMOXED", { SearchStartPosition::Begin, true, false })); + CPPUNIT_ASSERT_EQUAL(false, aSearch.next()); + } +} + CPPUNIT_TEST_SUITE_REGISTRATION(VectorGraphicSearchTest); #endif diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 6ca73280605d..e2d1d6d13123 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -35,7 +35,7 @@ public: sal_Int32 mnPageIndex; int mnCurrentIndex; OUString maSearchString; - SearchStartPosition meStartPosition; + VectorGraphicSearchOptions maOptions; SearchContext(FPDF_DOCUMENT pPdfDocument, sal_Int32 nPageIndex) : mpPdfDocument(pPdfDocument) @@ -44,7 +44,6 @@ public: , mpSearchHandle(nullptr) , mnPageIndex(nPageIndex) , mnCurrentIndex(-1) - , meStartPosition(SearchStartPosition::Begin) { } @@ -74,7 +73,7 @@ public: return aSize; } - bool initialize(OUString const& rSearchString, SearchStartPosition eStartPosition) + bool initialize(OUString const& rSearchString, VectorGraphicSearchOptions const& rOptions) { if (!mpPdfDocument) return false; @@ -92,7 +91,7 @@ public: FPDF_ClosePage(mpPage); maSearchString = rSearchString; - meStartPosition = eStartPosition; + maOptions = rOptions; mpPage = FPDF_LoadPage(mpPdfDocument, mnPageIndex); if (!mpPage) @@ -105,7 +104,7 @@ public: FPDF_WIDESTRING pString = reinterpret_cast<FPDF_WIDESTRING>(maSearchString.getStr()); // Index where to start to search. -1 => at the end - int nStartIndex = meStartPosition == SearchStartPosition::End ? -1 : 0; + int nStartIndex = maOptions.meStartPosition == SearchStartPosition::End ? -1 : 0; if (mnCurrentIndex >= 0) nStartIndex = mnCurrentIndex; @@ -115,6 +114,10 @@ public: // 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; + if (maOptions.mbMatchCase) + nSearchFlags |= FPDF_MATCHCASE; + if (maOptions.mbMatchWholeWord) + nSearchFlags |= FPDF_MATCHWHOLEWORD; mpSearchHandle = FPDFText_FindStart(mpTextPage, pString, nSearchFlags, nStartIndex); @@ -227,7 +230,8 @@ VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic) VectorGraphicSearch::~VectorGraphicSearch() { mpImplementation.reset(); } -bool VectorGraphicSearch::search(OUString const& rSearchString, SearchStartPosition eStartPosition) +bool VectorGraphicSearch::search(OUString const& rSearchString, + VectorGraphicSearchOptions const& rOptions) { if (!mpImplementation->mpSearchContext) { @@ -237,12 +241,12 @@ bool VectorGraphicSearch::search(OUString const& rSearchString, SearchStartPosit { if (searchPDF(pData)) { - return mpImplementation->mpSearchContext->initialize(rSearchString, eStartPosition); + return mpImplementation->mpSearchContext->initialize(rSearchString, rOptions); } } return false; } - return mpImplementation->mpSearchContext->initialize(rSearchString, eStartPosition); + return mpImplementation->mpSearchContext->initialize(rSearchString, rOptions); } bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& rData) @@ -334,7 +338,7 @@ VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic) VectorGraphicSearch::~VectorGraphicSearch() {} bool VectorGraphicSearch::search(OUString const& /*rSearchString*/, - SearchStartPosition /*eStartPosition*/) + VectorGraphicSearchOptions const& /*rOptions*/) { return false; } |