Commit 2b66dacb authored by Volker Krause's avatar Volker Krause
Browse files

Add API for extractor scripts to query PDF links in a specific sub rect

parent 29942f8f
......@@ -86,10 +86,17 @@ private Q_SLOTS:
QCOMPARE(doc->pageCount(), 1);
const auto page = doc->page(0);
QCOMPARE(page.linkCount(), 1);
const auto link = page.link(0);
auto link = page.link(0);
QCOMPARE(link.url(), QLatin1String("https://kde.org"));
qDebug() << link.area();
QVERIFY(link.area().isValid());
QCOMPARE(page.linksInRect(0.0, 0.0, 1.0, 1.0).size(), 1);
QCOMPARE(page.linksInRect(0.0, 0.0, 1.0, 0.5).size(), 1);
link = page.linksInRect(0.0, 0.0, 0.5, 0.5).at(0).value<PdfLink>();
QCOMPARE(link.url(), QLatin1String("https://kde.org"));
QCOMPARE(page.linksInRect(0.0, 0.0, 0.2, 0.5).size(), 1);
QCOMPARE(page.linksInRect(0.0, 0.5, 1.0, 1.0).size(), 0);
}
void testInvalidPdfDocument()
......
......@@ -175,6 +175,31 @@ QVariantList PdfPage::linksVariant() const
return l;
}
QVariantList PdfPage::linksInRect(double left, double top, double right, double bottom) const
{
QRectF bbox(QPointF(left, top), QPointF(right, bottom));
d->load();
QVariantList l;
for (const auto &link : d->m_links) {
if (!link.area().intersects(bbox)) {
continue;
}
l.push_back(QVariant::fromValue(link));
}
std::sort(l.begin(), l.end(), [](const auto &lhs, const auto &rhs) {
const auto lhsLink = lhs.template value<PdfLink>();
const auto rhsLink = rhs.template value<PdfLink>();
if (lhsLink.area().top() == rhsLink.area().top()) {
return lhsLink.area().left() < rhsLink.area().left();
}
return lhsLink.area().top() < rhsLink.area().top();
});
return l;
}
PdfDocument::PdfDocument(QObject *parent)
: QObject(parent)
......
......@@ -62,6 +62,12 @@ public:
/** The n-th link found in this document. */
PdfLink link(int index) const;
/** Returns all links in the specified sub-rect of this page.
* All parameters are relative values between @c 0 and @c 1 of the entire page size.
* Links are sorted top to bottom / left to right based on their top left corner.
*/
Q_INVOKABLE QVariantList linksInRect(double left, double top, double right, double bottom) const;
private:
QVariantList imagesVariant() const;
QVariantList linksVariant() const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment