diff --git a/thumbnail/CMakeLists.txt b/thumbnail/CMakeLists.txt --- a/thumbnail/CMakeLists.txt +++ b/thumbnail/CMakeLists.txt @@ -20,6 +20,11 @@ endif() endif() +find_package(Poppler "0.12.1" COMPONENTS Qt5) +set_package_properties(Poppler PROPERTIES DESCRIPTION "A PDF rendering library" + URL "https://poppler.freedesktop.org/" TYPE OPTIONAL + PURPOSE "Provides support for PDF thumbnails") + include_directories(${CMAKE_BINARY_DIR}) ########### next target ############### @@ -264,6 +269,24 @@ install(TARGETS ebookthumbnail DESTINATION ${KDE_INSTALL_PLUGINDIR}) +# ########### next target ############### + +if(Poppler_Qt5_FOUND) + +set(pdfthumbnail_SRCS pdfcreator.cpp) +add_library(pdfthumbnail MODULE ${pdfthumbnail_SRCS}) + +target_link_libraries(pdfthumbnail + Qt5::Gui + KF5::KIOWidgets + Poppler::Qt5 +) + +install(TARGETS pdfthumbnail DESTINATION ${KDE_INSTALL_PLUGINDIR}) +install(FILES pdfthumbnail.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) + +endif() + ########### install files ############### install(FILES thumbcreator.desktop DESTINATION ${KDE_INSTALL_KSERVICETYPES5DIR}) diff --git a/thumbnail/pdfcreator.h b/thumbnail/pdfcreator.h new file mode 100644 --- /dev/null +++ b/thumbnail/pdfcreator.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Kai Uwe Broulik + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#pragma once + +#include + +class PdfCreator : public ThumbCreator +{ +public: + PdfCreator(); + ~PdfCreator() override; + + bool create(const QString &path, int width, int height, QImage &image) override; + +}; diff --git a/thumbnail/pdfcreator.cpp b/thumbnail/pdfcreator.cpp new file mode 100644 --- /dev/null +++ b/thumbnail/pdfcreator.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019 Kai Uwe Broulik + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "pdfcreator.h" + +#include +#include + +#include + +extern "C" +{ + Q_DECL_EXPORT ThumbCreator *new_creator() + { + return new PdfCreator; + } +} + +using namespace Poppler; + +PdfCreator::PdfCreator() = default; + +PdfCreator::~PdfCreator() = default; + +bool PdfCreator::create(const QString &path, int width, int height, QImage &image) +{ + QScopedPointer document; + document.reset(Document::load(path)); + + if (!document || document->isLocked()) { + return false; + } + + document->setRenderHint(Poppler::Document::Antialiasing); + document->setRenderHint(Poppler::Document::TextAntialiasing); + + QScopedPointer page; + page.reset(document->page(0)); + + if (!page) { + return false; + } + + // Compute a dpi that results in a thumbnail roughly the size we requested + const qreal fakeDpiX = width / page->pageSizeF().width() * 72.0; + const qreal fakeDpiY = height / page->pageSizeF().height() * 72.0; + + // Preserve aspect fit + const qreal fakeDpi = std::min(fakeDpiX, fakeDpiY); + + image = page->renderToImage(fakeDpi, fakeDpi); + + return !image.isNull(); +} diff --git a/thumbnail/pdfthumbnail.desktop b/thumbnail/pdfthumbnail.desktop new file mode 100644 --- /dev/null +++ b/thumbnail/pdfthumbnail.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Type=Service +Name=PDF Documents + +X-KDE-ServiceTypes=ThumbCreator +MimeType=application/pdf; + +X-KDE-Library=pdfthumbnail +CacheThumbnail=true