include/vcl/VectorGraphicSearch.hxx        |    3 -
 sd/source/ui/view/Outliner.cxx             |   16 ++++-
 vcl/qa/cppunit/VectorGraphicSearchTest.cxx |   33 ++++++++++++
 vcl/source/graphic/VectorGraphicSearch.cxx |   79 +++++++++++++++++++----------
 4 files changed, 97 insertions(+), 34 deletions(-)

New commits:
commit 2a394db043293117213fcdf8615d7ce35a515464
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Sun May 31 21:59:34 2020 +0200
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Fri Jul 31 05:30:04 2020 +0200

    sd: allow to change the search string between searches
    
    Before this was missing, so even with a different search string,
    it still searched using the old string, which was a bug.
    
    Change-Id: I1655cb421e216e30ae593aabd3ead3a2d5c06299
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95461
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit 191288d6a7fb52b31038a21c4e71ee57ffa3bacd)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95947
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    (cherry picked from commit ca214ec3f5b3c65c8d6d335d284b0702f29b4c25)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99816

diff --git a/sd/source/ui/view/Outliner.cxx b/sd/source/ui/view/Outliner.cxx
index 8365fb47a1ff..d0128ac32cc0 100644
--- a/sd/source/ui/view/Outliner.cxx
+++ b/sd/source/ui/view/Outliner.cxx
@@ -854,13 +854,19 @@ bool 
SdOutliner::SearchAndReplaceOnce(std::vector<sd::SearchSelection>* pSelecti
 
             if (mpImpl->mbCurrentIsVectorGraphic)
             {
+                OUString const & rString = mpSearchItem->GetSearchString();
                 bool bBackwards = mpSearchItem->GetBackward();
 
-                bool bResult = false;
-                if (bBackwards)
-                    bResult = mpImpl->mpVectorGraphicSearch->previous();
-                else
-                    bResult = mpImpl->mpVectorGraphicSearch->next();
+                SearchStartPosition eSearchStartPosition = bBackwards ? 
SearchStartPosition::End : SearchStartPosition::Begin;
+                bool bResult = mpImpl->mpVectorGraphicSearch->search(rString, 
eSearchStartPosition);
+
+                if (bResult)
+                {
+                    if (bBackwards)
+                        bResult = mpImpl->mpVectorGraphicSearch->previous();
+                    else
+                        bResult = mpImpl->mpVectorGraphicSearch->next();
+                }
 
                 if (bResult)
                 {
commit 6af21e08548cfadd994b5a05c52e565fcfaf98d6
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Sun May 31 14:03:36 2020 +0200
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Fri Jul 31 05:29:52 2020 +0200

    vcl: VectorGraphicSearch - support changing search string
    
    Initial implementation only allowed to set the search string once.
    This change allows to change the search string and still retain
    the last position of a found string, so the search continues
    from this positon forward or backwards. This mimicks how we search
    through the GUI (which is the main use for this functionallity
    anyway).
    
    Change-Id: I8a7aee4b6b6525f483f105feaa1f83c4a0ad9594
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95460
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit 1f8a46ae50c6977add4c4116f114df3a58796be3)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95946
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    (cherry picked from commit 5713b22bc3b7bc54d83fc99616912504ebf63649)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99815

diff --git a/include/vcl/VectorGraphicSearch.hxx 
b/include/vcl/VectorGraphicSearch.hxx
index 2dc8cca3b76a..c9faaa51f1c9 100644
--- a/include/vcl/VectorGraphicSearch.hxx
+++ b/include/vcl/VectorGraphicSearch.hxx
@@ -32,8 +32,7 @@ private:
     std::unique_ptr<Implementation> mpImplementation;
     Graphic maGraphic;
 
-    bool searchPDF(std::shared_ptr<VectorGraphicData> const& rData, OUString 
const& rSearchString,
-                   SearchStartPosition eStartPosition);
+    bool searchPDF(std::shared_ptr<VectorGraphicData> const& rData);
 
 public:
     VectorGraphicSearch(Graphic const& rGraphic);
diff --git a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx 
b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx
index 5f65b4ba7e3d..8dbdcac0e2e1 100644
--- a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx
+++ b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx
@@ -27,10 +27,12 @@ class VectorGraphicSearchTest : public 
test::BootstrapFixtureBase
 
     void test();
     void testNextPrevious();
+    void testSearchStringChange();
 
     CPPUNIT_TEST_SUITE(VectorGraphicSearchTest);
     CPPUNIT_TEST(test);
     CPPUNIT_TEST(testNextPrevious);
+    CPPUNIT_TEST(testSearchStringChange);
     CPPUNIT_TEST_SUITE_END();
 };
 
@@ -160,6 +162,37 @@ void VectorGraphicSearchTest::testNextPrevious()
     }
 }
 
+void VectorGraphicSearchTest::testSearchStringChange()
+{
+    OUString aURL = getFullUrl("Pangram.pdf");
+    SvFileStream aStream(aURL, StreamMode::READ);
+    GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
+    Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream);
+    aGraphic.makeAvailable();
+
+    VectorGraphicSearch aSearch(aGraphic);
+
+    // Set search to "lazy"
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.search("lazy"));
+
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.next());
+    CPPUNIT_ASSERT_EQUAL(34, aSearch.index());
+
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.next());
+    CPPUNIT_ASSERT_EQUAL(817, aSearch.index());
+
+    // Change search to "fox"
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.search("fox"));
+
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.next());
+    CPPUNIT_ASSERT_EQUAL(822, aSearch.index());
+
+    // Change search to "Quick"
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.search("Quick"));
+    CPPUNIT_ASSERT_EQUAL(true, aSearch.previous());
+    CPPUNIT_ASSERT_EQUAL(784, aSearch.index());
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(VectorGraphicSearchTest);
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx 
b/vcl/source/graphic/VectorGraphicSearch.cxx
index a719329b7708..6ca73280605d 100644
--- a/vcl/source/graphic/VectorGraphicSearch.cxx
+++ b/vcl/source/graphic/VectorGraphicSearch.cxx
@@ -33,18 +33,18 @@ private:
 
 public:
     sal_Int32 mnPageIndex;
+    int mnCurrentIndex;
     OUString maSearchString;
     SearchStartPosition meStartPosition;
 
-    SearchContext(FPDF_DOCUMENT pPdfDocument, sal_Int32 nPageIndex, OUString 
const& rSearchString,
-                  SearchStartPosition eStartPosition)
+    SearchContext(FPDF_DOCUMENT pPdfDocument, sal_Int32 nPageIndex)
         : mpPdfDocument(pPdfDocument)
         , mpPage(nullptr)
         , mpTextPage(nullptr)
         , mpSearchHandle(nullptr)
         , mnPageIndex(nPageIndex)
-        , maSearchString(rSearchString)
-        , meStartPosition(eStartPosition)
+        , mnCurrentIndex(-1)
+        , meStartPosition(SearchStartPosition::Begin)
     {
     }
 
@@ -74,13 +74,30 @@ public:
         return aSize;
     }
 
-    bool initialize()
+    bool initialize(OUString const& rSearchString, SearchStartPosition 
eStartPosition)
     {
         if (!mpPdfDocument)
             return false;
+
+        if (rSearchString == maSearchString)
+            return true;
+
+        if (mpSearchHandle)
+            FPDFText_FindClose(mpSearchHandle);
+
+        if (mpTextPage)
+            FPDFText_ClosePage(mpTextPage);
+
+        if (mpPage)
+            FPDF_ClosePage(mpPage);
+
+        maSearchString = rSearchString;
+        meStartPosition = eStartPosition;
+
         mpPage = FPDF_LoadPage(mpPdfDocument, mnPageIndex);
         if (!mpPage)
             return false;
+
         mpTextPage = FPDFText_LoadPage(mpPage);
         if (!mpTextPage)
             return false;
@@ -90,6 +107,9 @@ public:
         // Index where to start to search. -1 => at the end
         int nStartIndex = meStartPosition == SearchStartPosition::End ? -1 : 0;
 
+        if (mnCurrentIndex >= 0)
+            nStartIndex = mnCurrentIndex;
+
         // 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.
@@ -103,15 +123,21 @@ public:
 
     bool next()
     {
-        if (mpSearchHandle)
-            return FPDFText_FindNext(mpSearchHandle);
+        if (mpSearchHandle && FPDFText_FindNext(mpSearchHandle))
+        {
+            mnCurrentIndex = index();
+            return true;
+        }
         return false;
     }
 
     bool previous()
     {
-        if (mpSearchHandle)
-            return FPDFText_FindPrev(mpSearchHandle);
+        if (mpSearchHandle && FPDFText_FindPrev(mpSearchHandle))
+        {
+            mnCurrentIndex = index();
+            return true;
+        }
         return false;
     }
 
@@ -203,22 +229,24 @@ VectorGraphicSearch::~VectorGraphicSearch() { 
mpImplementation.reset(); }
 
 bool VectorGraphicSearch::search(OUString const& rSearchString, 
SearchStartPosition eStartPosition)
 {
-    auto pData = maGraphic.getVectorGraphicData();
-
-    if (pData && pData->getVectorGraphicDataType() == 
VectorGraphicDataType::Pdf)
+    if (!mpImplementation->mpSearchContext)
     {
-        return searchPDF(pData, rSearchString, eStartPosition);
+        auto pData = maGraphic.getVectorGraphicData();
+
+        if (pData && pData->getVectorGraphicDataType() == 
VectorGraphicDataType::Pdf)
+        {
+            if (searchPDF(pData))
+            {
+                return 
mpImplementation->mpSearchContext->initialize(rSearchString, eStartPosition);
+            }
+        }
+        return false;
     }
-    return false;
+    return mpImplementation->mpSearchContext->initialize(rSearchString, 
eStartPosition);
 }
 
-bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& 
rData,
-                                    OUString const& rSearchString,
-                                    SearchStartPosition eStartPosition)
+bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& 
rData)
 {
-    if (rSearchString.isEmpty())
-        return false;
-
     mpImplementation->mpPdfDocument
         = 
FPDF_LoadMemDocument(rData->getVectorGraphicDataArray().getConstArray(),
                                rData->getVectorGraphicDataArrayLength(), 
/*password=*/nullptr);
@@ -250,10 +278,9 @@ bool 
VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& rD
 
     sal_Int32 nPageIndex = std::max(rData->getPageIndex(), sal_Int32(0));
 
-    mpImplementation->mpSearchContext.reset(new SearchContext(
-        mpImplementation->mpPdfDocument, nPageIndex, rSearchString, 
eStartPosition));
-
-    return mpImplementation->mpSearchContext->initialize();
+    mpImplementation->mpSearchContext.reset(
+        new SearchContext(mpImplementation->mpPdfDocument, nPageIndex));
+    return true;
 }
 
 basegfx::B2DSize VectorGraphicSearch::pageSize()
@@ -312,9 +339,7 @@ bool VectorGraphicSearch::search(OUString const& 
/*rSearchString*/,
     return false;
 }
 
-bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& 
/*rData*/,
-                                    OUString const& /*rSearchString*/,
-                                    SearchStartPosition /*eStartPosition*/)
+bool VectorGraphicSearch::searchPDF(std::shared_ptr<VectorGraphicData> const& 
/*rData*/)
 {
     return false;
 }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to