diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-11-14 14:47:45 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-11-14 14:49:01 +0100 |
commit | 74f6b25548d073972215e4f174405434583e3f5c (patch) | |
tree | c2c7a18de4d59d65d77c25f1eeb459442db8bd73 | |
parent | 6b25e22685cfe434bb17db006c33db7341ca7028 (diff) | |
parent | 6e79d033a30f9d56b72a5aa4cc9a02cf656e2fe5 (diff) |
Merge remote-tracking branch 'origin/5.13' into 5.14
Change-Id: Id70f0288cb1f2df758c9fd21f02a5c037594b25c
-rw-r--r-- | src/core/net/url_request_notification.cpp | 1 | ||||
-rw-r--r-- | src/core/printing/pdfium_document_wrapper_qt.cpp | 89 | ||||
-rw-r--r-- | src/core/printing/pdfium_document_wrapper_qt.h | 5 | ||||
-rw-r--r-- | src/webengine/doc/src/qtwebengine-features.qdoc | 3 | ||||
-rw-r--r-- | src/webenginewidgets/printer_worker.cpp | 42 |
5 files changed, 43 insertions, 97 deletions
diff --git a/src/core/net/url_request_notification.cpp b/src/core/net/url_request_notification.cpp index fa6d61f65..279bd5077 100644 --- a/src/core/net/url_request_notification.cpp +++ b/src/core/net/url_request_notification.cpp @@ -49,6 +49,7 @@ #include "profile_io_data_qt.h" #include "qwebengineurlrequestinfo_p.h" #include "type_conversion.h" +#include <QVariant> namespace QtWebEngineCore { 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/webengine/doc/src/qtwebengine-features.qdoc b/src/webengine/doc/src/qtwebengine-features.qdoc index 22eb72408..d761e910c 100644 --- a/src/webengine/doc/src/qtwebengine-features.qdoc +++ b/src/webengine/doc/src/qtwebengine-features.qdoc @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -37,6 +37,7 @@ \li \l{Audio and Video Codecs} \li \l{Chromium DevTools} \li \l{Client Certificates} + \li \l{Custom Schemes} \li \l{Drag and Drop} \li \l{Fullscreen} \li \l{HTML5 DRM} 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(); |