summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichal Klocek <michal.klocek@qt.io>2019-10-21 16:54:26 +0200
committerMichal Klocek <michal.klocek@qt.io>2019-10-31 18:43:23 +0100
commit5e4487c2d9c72c54269db5fe8f333c75479ccb5f (patch)
treefdd19e39c099f94519928528de522016381d1c27 /src
parent91fc86bef3b9eb9467f6ac251d1953483da93185 (diff)
Redo scale handling of pdf when printing
Since 7f89bad our printer handling produces low quality images, the reason for that is the we first render with pdfium to bitmap which is fixed to twice size of pdf document in points, then it is downscaled/upscaled by qpainter. This issue is even more visible when print preview is requested on hdpi screen. Simplify the code and do the "scaling" already when rendering bitmap with pdfium. Task-number: QTBUG-75092 Fixes: QTBUG-78161 Change-Id: I616ad1f371e7d3457a04b0a254603ed235c387bc Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/core/printing/pdfium_document_wrapper_qt.cpp89
-rw-r--r--src/core/printing/pdfium_document_wrapper_qt.h5
-rw-r--r--src/webenginewidgets/printer_worker.cpp42
3 files changed, 40 insertions, 96 deletions
diff --git a/src/core/printing/pdfium_document_wrapper_qt.cpp b/src/core/printing/pdfium_document_wrapper_qt.cpp
index 6f415b50b..dae6ec44b 100644
--- a/src/core/printing/pdfium_document_wrapper_qt.cpp
+++ b/src/core/printing/pdfium_document_wrapper_qt.cpp
@@ -48,64 +48,6 @@
namespace QtWebEngineCore {
int PdfiumDocumentWrapperQt::m_libraryUsers = 0;
-class PdfiumPageWrapperQt {
-public:
- PdfiumPageWrapperQt(FPDF_DOCUMENT data, int pageIndex)
- : m_pageData(FPDF_LoadPage(data, pageIndex))
- , m_width(FPDF_GetPageWidth(m_pageData))
- , m_height(FPDF_GetPageHeight(m_pageData))
- , m_image(createImage())
- {
- }
-
- PdfiumPageWrapperQt()
- : m_pageData(nullptr)
- , m_width(-1)
- , m_height(-1)
- , m_image(QImage())
- {
- }
-
- virtual ~PdfiumPageWrapperQt()
- {
- FPDF_ClosePage(m_pageData);
- }
-
- QImage image()
- {
- return m_image;
- }
-
-private:
- QImage createImage()
- {
- Q_ASSERT(m_pageData);
-
- QImage image(m_width * 2, m_height * 2, QImage::Format_ARGB32);
- Q_ASSERT(!image.isNull());
- image.fill(0xFFFFFFFF);
-
- FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(image.width(), image.height(),
- FPDFBitmap_BGRA,
- image.scanLine(0), image.bytesPerLine());
- Q_ASSERT(bitmap);
-
- FPDF_RenderPageBitmap(bitmap, m_pageData,
- 0, 0, image.width(), image.height(),
- 0, 0);
- FPDFBitmap_Destroy(bitmap);
- bitmap = nullptr;
- return image;
- }
-
-private:
- FPDF_PAGE m_pageData;
- int m_width;
- int m_height;
- QImage m_image;
-};
-
-
PdfiumDocumentWrapperQt::PdfiumDocumentWrapperQt(const void *pdfData, size_t size,
const char *password)
{
@@ -118,27 +60,41 @@ PdfiumDocumentWrapperQt::PdfiumDocumentWrapperQt(const void *pdfData, size_t siz
m_pageCount = FPDF_GetPageCount((FPDF_DOCUMENT)m_documentHandle);
}
-QImage PdfiumDocumentWrapperQt::pageAsQImage(size_t index)
+QImage PdfiumDocumentWrapperQt::pageAsQImage(size_t pageIndex,int width , int height)
{
if (!m_documentHandle || !m_pageCount) {
qWarning("Failure to generate QImage from invalid or empty PDF document.");
return QImage();
}
- if (static_cast<int>(index) >= m_pageCount) {
+ if (static_cast<int>(pageIndex) >= m_pageCount) {
qWarning("Failure to generate QImage from PDF data: index out of bounds.");
return QImage();
}
- PdfiumPageWrapperQt pageWrapper((FPDF_DOCUMENT)m_documentHandle, index);
- return pageWrapper.image();
+ FPDF_PAGE pageData(FPDF_LoadPage((FPDF_DOCUMENT)m_documentHandle, pageIndex));
+ QImage image(width, height, QImage::Format_ARGB32);
+ Q_ASSERT(!image.isNull());
+ image.fill(0xFFFFFFFF);
+
+ FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(width, height,
+ FPDFBitmap_BGRA,
+ image.scanLine(0), image.bytesPerLine());
+ Q_ASSERT(bitmap);
+ FPDF_RenderPageBitmap(bitmap, pageData,
+ 0, 0, width, height,
+ 0, 0);
+ FPDFBitmap_Destroy(bitmap);
+ bitmap = nullptr;
+ FPDF_ClosePage(pageData);
+ return image;
}
-bool PdfiumDocumentWrapperQt::pageIsLandscape(size_t index)
+QSizeF PdfiumDocumentWrapperQt::pageSize(size_t index)
{
- double width = 0, height = 0;
- FPDF_GetPageSizeByIndex((FPDF_DOCUMENT)m_documentHandle, index, &width, &height);
- return (width > height);
+ QSizeF size;
+ FPDF_GetPageSizeByIndex((FPDF_DOCUMENT)m_documentHandle, index, &size.rwidth(), &size.rheight());
+ return size;
}
PdfiumDocumentWrapperQt::~PdfiumDocumentWrapperQt()
@@ -147,5 +103,4 @@ PdfiumDocumentWrapperQt::~PdfiumDocumentWrapperQt()
if (--m_libraryUsers == 0)
FPDF_DestroyLibrary();
}
-
}
diff --git a/src/core/printing/pdfium_document_wrapper_qt.h b/src/core/printing/pdfium_document_wrapper_qt.h
index 121742aa3..e0778c32b 100644
--- a/src/core/printing/pdfium_document_wrapper_qt.h
+++ b/src/core/printing/pdfium_document_wrapper_qt.h
@@ -56,15 +56,14 @@
#include <QtGui/qimage.h>
namespace QtWebEngineCore {
-class PdfiumPageWrapperQt;
class Q_WEBENGINECORE_PRIVATE_EXPORT PdfiumDocumentWrapperQt
{
public:
PdfiumDocumentWrapperQt(const void *pdfData, size_t size, const char *password = nullptr);
virtual ~PdfiumDocumentWrapperQt();
- QImage pageAsQImage(size_t index);
- bool pageIsLandscape(size_t index);
+ QImage pageAsQImage(size_t index, int width , int height);
+ QSizeF pageSize(size_t index);
int pageCount() const { return m_pageCount; }
private:
diff --git a/src/webenginewidgets/printer_worker.cpp b/src/webenginewidgets/printer_worker.cpp
index 94a862cda..8e1c2a985 100644
--- a/src/webenginewidgets/printer_worker.cpp
+++ b/src/webenginewidgets/printer_worker.cpp
@@ -94,17 +94,9 @@ void PrinterWorker::print()
documentCopies = 1;
}
- bool isLandscape = pdfiumWrapper.pageIsLandscape(0);
- QPageLayout::Orientation prevOrientation = m_printer->pageLayout().orientation();
- m_printer->setPageOrientation(isLandscape ? QPageLayout::Landscape : QPageLayout::Portrait);
+ qreal resolution = m_printer->resolution() / 72.0; // pdfium uses points so 1/72 inch
QPainter painter;
- if (!painter.begin(m_printer)) {
- qWarning("Failure to print on printer %ls: Could not open printer for painting.",
- qUtf16Printable(m_printer->printerName()));
- Q_EMIT resultReady(false);
- return;
- }
for (int printedDocuments = 0; printedDocuments < documentCopies; printedDocuments++) {
if (printedDocuments > 0)
@@ -113,11 +105,20 @@ void PrinterWorker::print()
int currentPageIndex = fromPage;
for (int i = 0; true; i++) {
- prevOrientation = m_printer->pageLayout().orientation();
- isLandscape = pdfiumWrapper.pageIsLandscape(currentPageIndex - 1);
+ QSizeF documentSize = (pdfiumWrapper.pageSize(currentPageIndex - 1) * resolution);
+ bool isLandscape = documentSize.width() > documentSize.height();
m_printer->setPageOrientation(isLandscape ? QPageLayout::Landscape : QPageLayout::Portrait);
-
- QSize pageSize = m_printer->pageRect().size();
+ QRectF pageRect = m_printer->pageRect(QPrinter::DevicePixel);
+ documentSize = documentSize.scaled(pageRect.size(), Qt::KeepAspectRatio);
+
+ // setPageOrientation has to be called before qpainter.begin() or before qprinter.newPage() so correct metrics is used,
+ // therefore call begin now for only first page
+ if (!painter.isActive() && !painter.begin(m_printer)) {
+ qWarning("Failure to print on printer %ls: Could not open printer for painting.",
+ qUtf16Printable(m_printer->printerName()));
+ Q_EMIT resultReady(false);
+ return;
+ }
if (i > 0)
m_printer->newPage();
@@ -132,21 +133,12 @@ void PrinterWorker::print()
if (printedPages > 0)
m_printer->newPage();
- QImage currentImage = pdfiumWrapper.pageAsQImage(currentPageIndex - 1);
+ QImage currentImage = pdfiumWrapper.pageAsQImage(currentPageIndex - 1,documentSize.width(),documentSize.height());
if (currentImage.isNull()) {
Q_EMIT resultReady(false);
return;
}
-
- QRect targetRect = currentImage.rect();
- // Scale down currentImage by both width and height to fit into the drawable area of the page.
- float scaleFactor = (float)pageSize.width() / (float)targetRect.width();
- targetRect = QRect(0, 0, targetRect.width() * scaleFactor, targetRect.height() * scaleFactor);
- scaleFactor = (float)pageSize.height() / (float)targetRect.height();
- targetRect = QRect(0, 0, targetRect.width() * scaleFactor, targetRect.height() * scaleFactor);
-
- // Painting operations are automatically clipped to the bounds of the drawable part of the page.
- painter.drawImage(targetRect, currentImage, currentImage.rect());
+ painter.drawImage(0,0, currentImage);
}
if (currentPageIndex == toPage)
@@ -156,8 +148,6 @@ void PrinterWorker::print()
currentPageIndex++;
else
currentPageIndex--;
-
- m_printer->setPageOrientation(prevOrientation);
}
}
painter.end();