summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2020-06-04 18:26:58 +0200
committerTomaž Vajngerl <quikee@gmail.com>2020-06-04 22:48:58 +0200
commit112d8113388513d9c6b317e828f5d373b4a54330 (patch)
tree849068d25f39c0d49ec24ba5858235b99ee03df0
parent40c577f57fb16d24d5a2f76c3a5126073fff6a98 (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>
-rw-r--r--include/vcl/VectorGraphicSearch.hxx24
-rw-r--r--sd/source/ui/view/Outliner.cxx16
-rw-r--r--vcl/qa/cppunit/VectorGraphicSearchTest.cxx88
-rw-r--r--vcl/source/graphic/VectorGraphicSearch.cxx22
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 f8cec9464896..9b777d3a1dad 100644
--- a/sd/source/ui/view/Outliner.cxx
+++ b/sd/source/ui/view/Outliner.cxx
@@ -829,8 +829,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)
{
@@ -1242,11 +1246,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 95064407f553..529e8c89c489 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)
{
}
@@ -73,7 +72,7 @@ public:
return aSize;
}
- bool initialize(OUString const& rSearchString, SearchStartPosition eStartPosition)
+ bool initialize(OUString const& rSearchString, VectorGraphicSearchOptions const& rOptions)
{
if (!mpPdfDocument)
return false;
@@ -91,7 +90,7 @@ public:
FPDF_ClosePage(mpPage);
maSearchString = rSearchString;
- meStartPosition = eStartPosition;
+ maOptions = rOptions;
mpPage = FPDF_LoadPage(mpPdfDocument, mnPageIndex);
if (!mpPage)
@@ -104,7 +103,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;
@@ -114,6 +113,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);
@@ -226,7 +229,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)
{
@@ -236,12 +240,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)
@@ -333,7 +337,7 @@ VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic)
VectorGraphicSearch::~VectorGraphicSearch() {}
bool VectorGraphicSearch::search(OUString const& /*rSearchString*/,
- SearchStartPosition /*eStartPosition*/)
+ VectorGraphicSearchOptions const& /*rOptions*/)
{
return false;
}