From edcd992697e1013309096787723a211a9e197bd4 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Thu, 29 Jul 2021 20:20:28 +0200 Subject: [PATCH] DocPage Organizer: page image preview --- Pdf4QtDocPageOrganizer/mainwindow.cpp | 10 +++++ Pdf4QtDocPageOrganizer/pageitemdelegate.cpp | 40 +++++++++++++++++++- Pdf4QtDocPageOrganizer/pageitemdelegate.h | 4 ++ Pdf4QtLib/sources/pdfdrawspacecontroller.cpp | 2 +- Pdf4QtLib/sources/pdfrenderer.cpp | 7 ++-- Pdf4QtLib/sources/pdfrenderer.h | 6 ++- 6 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Pdf4QtDocPageOrganizer/mainwindow.cpp b/Pdf4QtDocPageOrganizer/mainwindow.cpp index 2895b22..0456c2d 100644 --- a/Pdf4QtDocPageOrganizer/mainwindow.cpp +++ b/Pdf4QtDocPageOrganizer/mainwindow.cpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include namespace pdfdocpage { @@ -126,6 +128,14 @@ MainWindow::MainWindow(QWidget* parent) : } } + // Initialize pixmap cache size + const int depth = 4; // 4 bytes (ARGB) + const int reserveSize = 2; // Caching of two screens + QSize size = QGuiApplication::primaryScreen()->availableVirtualSize(); + int bytes = size.width() * size.height() * depth * reserveSize; + int kBytes = bytes / 1024; + QPixmapCache::setCacheLimit(kBytes); + loadSettings(); updateActions(); } diff --git a/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp b/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp index 748866e..f4226be 100644 --- a/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp +++ b/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp @@ -20,6 +20,8 @@ #include "pdfwidgetutils.h" #include "pdfpainterutils.h" #include "pdfrenderer.h" +#include "pdfcompiler.h" +#include "pdfconstants.h" #include #include @@ -29,9 +31,13 @@ namespace pdfdocpage PageItemDelegate::PageItemDelegate(PageItemModel* model, QObject* parent) : BaseClass(parent), - m_model(model) + m_model(model), + m_rasterizer(nullptr) { - + m_rasterizer = new pdf::PDFRasterizer(this); + QSurfaceFormat format; + format.setSamples(16); + m_rasterizer->reset(true, format); } PageItemDelegate::~PageItemDelegate() @@ -161,7 +167,37 @@ QPixmap PageItemDelegate::getPageImagePixmap(const PageGroupItem* item, QRect re switch (groupItem.pageType) { case pdfdocpage::PT_DocumentPage: + { + const auto& documents = m_model->getDocuments(); + auto it = documents.find(groupItem.documentIndex); + if (it != documents.cend()) + { + const pdf::PDFDocument& document = it->second.document; + const pdf::PDFInteger pageIndex = groupItem.pageIndex - 1; + if (pageIndex >= 0 && pageIndex < pdf::PDFInteger(document.getCatalog()->getPageCount())) + { + const pdf::PDFPage* page = document.getCatalog()->getPage(pageIndex); + Q_ASSERT(page); + + pdf::PDFPrecompiledPage compiledPage; + pdf::PDFFontCache fontCache(pdf::DEFAULT_FONT_CACHE_LIMIT, pdf::DEFAULT_REALIZED_FONT_CACHE_LIMIT); + pdf::PDFCMSManager cmsManager(nullptr); + pdf::PDFOptionalContentActivity optionalContentActivity(&document, pdf::OCUsage::View, nullptr); + + fontCache.setDocument(pdf::PDFModifiedDocument(const_cast(&document), &optionalContentActivity)); + cmsManager.setDocument(&document); + + pdf::PDFCMSPointer cms = cmsManager.getCurrentCMS(); + pdf::PDFRenderer renderer(&document, &fontCache, cms.data(), &optionalContentActivity, pdf::PDFRenderer::getDefaultFeatures(), pdf::PDFMeshQualitySettings()); + renderer.compile(&compiledPage, pageIndex); + + QSize imageSize = rect.size(); + QImage pageImage = m_rasterizer->render(pageIndex, page, &compiledPage, imageSize, pdf::PDFRenderer::getDefaultFeatures(), nullptr, groupItem.pageAdditionalRotation); + pixmap = QPixmap::fromImage(qMove(pageImage)); + } + } break; + } case pdfdocpage::PT_Image: { diff --git a/Pdf4QtDocPageOrganizer/pageitemdelegate.h b/Pdf4QtDocPageOrganizer/pageitemdelegate.h index 0f8f645..5fed611 100644 --- a/Pdf4QtDocPageOrganizer/pageitemdelegate.h +++ b/Pdf4QtDocPageOrganizer/pageitemdelegate.h @@ -18,6 +18,9 @@ #ifndef PDFDOCPAGEORGANIZER_PAGEITEMDELEGATE_H #define PDFDOCPAGEORGANIZER_PAGEITEMDELEGATE_H +#include "pdfrenderer.h" +#include "pdfcms.h" + #include namespace pdfdocpage @@ -51,6 +54,7 @@ private: PageItemModel* m_model; QSize m_pageImageSize; + pdf::PDFRasterizer* m_rasterizer; }; } // namespace pdfdocpage diff --git a/Pdf4QtLib/sources/pdfdrawspacecontroller.cpp b/Pdf4QtLib/sources/pdfdrawspacecontroller.cpp index 2000ed8..e5183fd 100644 --- a/Pdf4QtLib/sources/pdfdrawspacecontroller.cpp +++ b/Pdf4QtLib/sources/pdfdrawspacecontroller.cpp @@ -845,7 +845,7 @@ QImage PDFDrawWidgetProxy::drawThumbnailImage(PDFInteger pageIndex, int pixelSiz if (compiledPage && compiledPage->isValid()) { // Rasterize the image. - image = m_rasterizer->render(pageIndex, page, compiledPage, imageSize, m_features, m_widget->getAnnotationManager()); + image = m_rasterizer->render(pageIndex, page, compiledPage, imageSize, m_features, m_widget->getAnnotationManager(), PageRotation::None); } if (image.isNull()) diff --git a/Pdf4QtLib/sources/pdfrenderer.cpp b/Pdf4QtLib/sources/pdfrenderer.cpp index 92044f9..be0635f 100644 --- a/Pdf4QtLib/sources/pdfrenderer.cpp +++ b/Pdf4QtLib/sources/pdfrenderer.cpp @@ -209,11 +209,12 @@ QImage PDFRasterizer::render(PDFInteger pageIndex, const PDFPrecompiledPage* compiledPage, QSize size, PDFRenderer::Features features, - const PDFAnnotationManager* annotationManager) + const PDFAnnotationManager* annotationManager, + PageRotation extraRotation) { QImage image; - QMatrix matrix = PDFRenderer::createPagePointToDevicePointMatrix(page, QRect(QPoint(0, 0), size)); + QMatrix matrix = PDFRenderer::createPagePointToDevicePointMatrix(page, QRect(QPoint(0, 0), size), extraRotation); if (m_features.testFlag(UseOpenGL) && m_features.testFlag(ValidOpenGL)) { // We have valid OpenGL context, try to select it and possibly create framebuffer object @@ -464,7 +465,7 @@ void PDFRasterizerPool::render(const std::vector& pageIndices, pageTimer.restart(); PDFRasterizer* rasterizer = acquire(); qint64 pageWaitTime = pageTimer.restart(); - QImage image = rasterizer->render(pageIndex, page, &precompiledPage, imageSizeGetter(page), m_features, &annotationManager); + QImage image = rasterizer->render(pageIndex, page, &precompiledPage, imageSizeGetter(page), m_features, &annotationManager, PageRotation::None); qint64 pageRenderTime = pageTimer.elapsed(); release(rasterizer); diff --git a/Pdf4QtLib/sources/pdfrenderer.h b/Pdf4QtLib/sources/pdfrenderer.h index 3baa00a..c520144 100644 --- a/Pdf4QtLib/sources/pdfrenderer.h +++ b/Pdf4QtLib/sources/pdfrenderer.h @@ -126,7 +126,7 @@ private: /// if it is enabled, if this is the case, offscreen rendering to framebuffer /// is used. /// \note Construct this object only in main GUI thread -class PDFRasterizer : public QObject +class Pdf4QtLIBSHARED_EXPORT PDFRasterizer : public QObject { Q_OBJECT @@ -163,12 +163,14 @@ public: /// \param size Size of the target image /// \param features Renderer features /// \param annotationManager Annotation manager (can be nullptr) + /// \param extraRotation Extra page rotation QImage render(PDFInteger pageIndex, const PDFPage* page, const PDFPrecompiledPage* compiledPage, QSize size, PDFRenderer::Features features, - const PDFAnnotationManager* annotationManager); + const PDFAnnotationManager* annotationManager, + PageRotation extraRotation); private: void initializeOpenGL();