2022-01-03 22:59:44 +01:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QPainter>
|
|
|
|
#include "previewgeneratorpdf.h"
|
|
|
|
|
|
|
|
static QMutex cacheMutex;
|
|
|
|
|
|
|
|
Poppler::Document *PreviewGeneratorPdf::document(QString path)
|
|
|
|
{
|
|
|
|
if(documentcache.contains(path))
|
|
|
|
{
|
|
|
|
return documentcache.value(path);
|
|
|
|
}
|
|
|
|
Poppler::Document *result = Poppler::Document::load(path);
|
|
|
|
if(result == nullptr)
|
|
|
|
{
|
|
|
|
// TODO: some kind of user feedback would be nice
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
result->setRenderHint(Poppler::Document::TextAntialiasing);
|
|
|
|
QMutexLocker locker(&cacheMutex);
|
|
|
|
documentcache.insert(path, result);
|
|
|
|
locker.unlock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-27 09:28:21 +02:00
|
|
|
QSharedPointer<PreviewResult> PreviewGeneratorPdf::generate(RenderConfig config, QString documentPath,
|
|
|
|
unsigned int page)
|
2022-01-03 22:59:44 +01:00
|
|
|
{
|
|
|
|
PreviewResultPdf *result = new PreviewResultPdf(documentPath, page);
|
|
|
|
Poppler::Document *doc = document(documentPath);
|
|
|
|
if(doc == nullptr)
|
|
|
|
{
|
2022-05-27 09:28:21 +02:00
|
|
|
return QSharedPointer<PreviewResult>(result);
|
2022-01-03 22:59:44 +01:00
|
|
|
}
|
|
|
|
if(doc->isLocked())
|
|
|
|
{
|
2022-05-27 09:28:21 +02:00
|
|
|
return QSharedPointer<PreviewResult>(result);
|
2022-01-03 22:59:44 +01:00
|
|
|
}
|
|
|
|
int p = (int)page - 1;
|
|
|
|
if(p < 0)
|
|
|
|
{
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
Poppler::Page *pdfPage = doc->page(p);
|
|
|
|
QImage img = pdfPage->renderToImage(config.scaleX, config.scaleY);
|
|
|
|
for(QString &word : config.wordsToHighlight)
|
|
|
|
{
|
|
|
|
QList<QRectF> rects = pdfPage->search(word, Poppler::Page::SearchFlag::IgnoreCase);
|
|
|
|
for(QRectF &rect : rects)
|
|
|
|
{
|
|
|
|
QPainter painter(&img);
|
|
|
|
painter.scale(config.scaleX / 72.0, config.scaleY / 72.0);
|
|
|
|
painter.fillRect(rect, QColor(255, 255, 0, 64));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result->previewImage = img;
|
2022-05-27 09:28:21 +02:00
|
|
|
return QSharedPointer<PreviewResult>(result);
|
2022-01-03 22:59:44 +01:00
|
|
|
}
|