Fix painter opacity being ignored when rendering to PDF

The opacity set on a QPainter was being ignored for drawImage()/drawPixmap()
calls when rendering to a PDF file through QPdfWriter/QPrinter. This change
fixes it for PDF files v1.4 (default version) or v1.6. For PDF/A-1b files,
opacity will remain ignored to ensure compliance with the specification.

Task-number: QTBUG-70752
Change-Id: I4280e898698e0367dfb4c6ac2cd14ca2bf98850e
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Andre de la Rocha 2018-10-25 18:18:57 +02:00
parent fc1ab49cd7
commit eb056ee233

View File

@ -953,7 +953,18 @@ void QPdfEngine::drawPixmap (const QRectF &rectangle, const QPixmap &pixmap, con
if (object < 0)
return;
*d->currentPage << "q\n/GSa gs\n";
*d->currentPage << "q\n";
if ((d->pdfVersion != QPdfEngine::Version_A1b) && (d->opacity != 1.0)) {
int stateObject = d->addConstantAlphaObject(qRound(255 * d->opacity), qRound(255 * d->opacity));
if (stateObject)
*d->currentPage << "/GState" << stateObject << "gs\n";
else
*d->currentPage << "/GSa gs\n";
} else {
*d->currentPage << "/GSa gs\n";
}
*d->currentPage
<< QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(),
rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix));
@ -981,7 +992,18 @@ void QPdfEngine::drawImage(const QRectF &rectangle, const QImage &image, const Q
if (object < 0)
return;
*d->currentPage << "q\n/GSa gs\n";
*d->currentPage << "q\n";
if ((d->pdfVersion != QPdfEngine::Version_A1b) && (d->opacity != 1.0)) {
int stateObject = d->addConstantAlphaObject(qRound(255 * d->opacity), qRound(255 * d->opacity));
if (stateObject)
*d->currentPage << "/GState" << stateObject << "gs\n";
else
*d->currentPage << "/GSa gs\n";
} else {
*d->currentPage << "/GSa gs\n";
}
*d->currentPage
<< QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(),
rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix));