Skip to content

Commit

Permalink
Add support for drawing a hyperlink in QPdfEngine
Browse files Browse the repository at this point in the history
Adds the drawHyperlink method so that clients can draw a hyperlink
pointing to the specified URL at the specified rectangle. That new
method is to be used by GraphicContext::setURLForRect implementations
that want to render anchors as clickable links in PDF documents.

Task-number: QTBUG-44563
Change-Id: I7b0c602da37ee157d18115c531ab1b11fe304c13
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
  • Loading branch information
astefanutti authored and vitallium committed Dec 12, 2015
1 parent 0441330 commit d50c481
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/gui/painting/qpdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,34 @@ void QPdfEngine::drawTextItem(const QPointF &p, const QTextItem &textItem)
*d->currentPage << "Q\n";
}

void QPdfEngine::drawHyperlink(const QRectF &r, const QUrl &url)
{
Q_D(QPdfEngine);

const uint annot = d->addXrefEntry(-1);
const QByteArray urlascii = url.toEncoded();
int len = urlascii.size();
QVarLengthArray<char> url_esc(0);
for (int j = 0; j < len; j++) {
if (urlascii[j] == '(' || urlascii[j] == ')' || urlascii[j] == '\\')
url_esc.append('\\');
url_esc.append(urlascii[j]);
}
url_esc.append('\0');

char buf[256];
const QRectF rr = d->pageMatrix().mapRect(r);
d->xprintf("<<\n/Type /Annot\n/Subtype /Link\n/Rect [");
d->xprintf("%s ", qt_real_to_string(rr.left(), buf));
d->xprintf("%s ", qt_real_to_string(rr.top(), buf));
d->xprintf("%s ", qt_real_to_string(rr.right(), buf));
d->xprintf("%s", qt_real_to_string(rr.bottom(), buf));
d->xprintf("]\n/Border [0 0 0]\n/A <<\n");
d->xprintf("/Type /Action\n/S /URI\n/URI (%s)\n", url_esc.constData());
d->xprintf(">>\n>>\n");
d->xprintf("endobj\n");
d->currentPage->annotations.append(annot);
}

void QPdfEngine::updateState(const QPaintEngineState &state)
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/painting/qpdf_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ class Q_GUI_EXPORT QPdfEngine : public QPaintEngine

void updateState(const QPaintEngineState &state) Q_DECL_OVERRIDE;

void drawHyperlink(const QRectF &r, const QUrl &url);

int metric(QPaintDevice::PaintDeviceMetric metricType) const;
Type type() const Q_DECL_OVERRIDE;
// end reimplementations QPaintEngine
Expand Down

0 comments on commit d50c481

Please sign in to comment.