From 4d823e2cfa3171a54f20c313d7670be8ed9fa604 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sat, 16 May 2020 19:45:41 +0200 Subject: vcl: VectorGraphicSearch - add search result selection rectangles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ia0c5610f600719bcfb5de503f3876fc896cb630a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95256 Tested-by: Tomaž Vajngerl Reviewed-by: Tomaž Vajngerl (cherry picked from commit 4062b3f87689e48fd250d9cf0297a24b5427bf59) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95833 --- vcl/qa/cppunit/VectorGraphicSearchTest.cxx | 23 ++++++++++++++ vcl/source/graphic/VectorGraphicSearch.cxx | 49 +++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) (limited to 'vcl') diff --git a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx index 0ed21ccf9e26..112748d23bbe 100644 --- a/vcl/qa/cppunit/VectorGraphicSearchTest.cxx +++ b/vcl/qa/cppunit/VectorGraphicSearchTest.cxx @@ -9,6 +9,7 @@ #include #include + #include #include @@ -43,6 +44,28 @@ void VectorGraphicSearchTest::test() CPPUNIT_ASSERT_EQUAL(true, aSearch.search("lazy")); CPPUNIT_ASSERT_EQUAL(true, aSearch.next()); CPPUNIT_ASSERT_EQUAL(34, aSearch.index()); + auto aRectangles = aSearch.getTextRectangles(); + CPPUNIT_ASSERT_EQUAL(size_t(4), aRectangles.size()); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(229.00, aRectangles[0].getMinX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(231.85, aRectangles[0].getMaxX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(724.10, aRectangles[0].getMinY(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(732.42, aRectangles[0].getMaxY(), 1E-2); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(232.47, aRectangles[1].getMinX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(237.22, aRectangles[1].getMaxX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(723.99, aRectangles[1].getMinY(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(729.72, aRectangles[1].getMaxY(), 1E-2); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(237.68, aRectangles[2].getMinX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(242.35, aRectangles[2].getMaxX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(724.09, aRectangles[2].getMinY(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(729.60, aRectangles[2].getMaxY(), 1E-2); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(242.81, aRectangles[3].getMinX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(248.61, aRectangles[3].getMaxX(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(721.51, aRectangles[3].getMinY(), 1E-2); + CPPUNIT_ASSERT_DOUBLES_EQUAL(729.60, aRectangles[3].getMaxY(), 1E-2); } CPPUNIT_TEST_SUITE_REGISTRATION(VectorGraphicSearchTest); diff --git a/vcl/source/graphic/VectorGraphicSearch.cxx b/vcl/source/graphic/VectorGraphicSearch.cxx index 53127b85d9c1..a00df2555fb5 100644 --- a/vcl/source/graphic/VectorGraphicSearch.cxx +++ b/vcl/source/graphic/VectorGraphicSearch.cxx @@ -8,9 +8,10 @@ * */ -#include #include +#include + #include #include @@ -93,6 +94,44 @@ public: return FPDFText_GetSchResultIndex(mpSearchHandle); return -1; } + + int size() + { + if (mpSearchHandle) + return FPDFText_GetSchCount(mpSearchHandle); + return -1; + } + + std::vector getTextRectangles() + { + std::vector aRectangles; + + if (!mpTextPage || !mpSearchHandle) + return aRectangles; + + int nIndex = index(); + if (nIndex < 0) + return aRectangles; + + int nSize = size(); + if (nSize <= 0) + return aRectangles; + + for (int nCount = 0; nCount < nSize; nCount++) + { + double left = 0.0; + double right = 0.0; + double bottom = 0.0; + double top = 0.0; + + if (FPDFText_GetCharBox(mpTextPage, nIndex + nCount, &left, &right, &bottom, &top)) + { + aRectangles.emplace_back(left, bottom, right, top); + } + } + + return aRectangles; + } }; VectorGraphicSearch::VectorGraphicSearch(Graphic const& rGraphic) @@ -182,4 +221,12 @@ int VectorGraphicSearch::index() return -1; } +std::vector VectorGraphicSearch::getTextRectangles() +{ + if (mpSearchContext) + return mpSearchContext->getTextRectangles(); + + return std::vector(); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit