From eb056ee233c80376a837beba8f712a0f1068dae3 Mon Sep 17 00:00:00 2001 From: Andre de la Rocha Date: Thu, 25 Oct 2018 18:18:57 +0200 Subject: 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 Reviewed-by: Lars Knoll Reviewed-by: Friedemann Kleint --- src/gui/painting/qpdf.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/gui/painting') diff --git a/src/gui/painting/qpdf.cpp b/src/gui/painting/qpdf.cpp index b410e9b900..e69726b617 100644 --- a/src/gui/painting/qpdf.cpp +++ b/src/gui/painting/qpdf.cpp @@ -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)); -- cgit v1.2.3