-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_pdf.cpp
More file actions
152 lines (129 loc) · 4.19 KB
/
Copy pathrender_pdf.cpp
File metadata and controls
152 lines (129 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Renderer for PDF documents.
* Copyright (c) 2021-2026 Benjamin Johnson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <memory> // for std::unique_ptr
#include <QtCore>
#include <poppler-qt6.h>
#include "render_pdf.h"
#include "renderer_util.h"
struct PDFRendererData {
std::unique_ptr<Poppler::Document> document;
};
static QString popplerError;
static QMutex popplerErrorMutex;
static void storePopplerError(const QString &message, const QVariant &closure);
void PDFRenderer::init()
{
Poppler::setDebugErrorFunction(&storePopplerError, QVariant());
}
PDFRenderer::PDFRenderer()
: PagedContentRenderer()
{
data = new PDFRendererData;
data->document = nullptr;
}
PDFRenderer::~PDFRenderer()
{
delete data;
}
bool PDFRenderer::load()
{
QMutexLocker locker(&popplerErrorMutex);
popplerError.clear();
data->document = Poppler::Document::load(path());
if (data->document == nullptr) {
storeLoadError(popplerError);
popplerError.clear();
return false;
} else
return true;
}
void PDFRenderer::renderPage(int num)
{
QMutexLocker locker(&popplerErrorMutex);
popplerError.clear();
if (!pageExists(num)) {
emit errorEncountered(popplerError);
popplerError.clear();
return;
}
// Make the document look nice on screen
data->document->setRenderHint(Poppler::Document::Antialiasing);
data->document->setRenderHint(Poppler::Document::TextAntialiasing);
std::unique_ptr<Poppler::Page> page = data->document->page(num);
if (page == nullptr) {
emit errorEncountered(popplerError);
popplerError.clear();
return;
}
int xRes = zoomScaled(dpiX()), yRes = zoomScaled(dpiY());
QImage image = page->renderToImage(xRes, yRes);
if (image.isNull()) {
emit errorEncountered(popplerError);
popplerError.clear();
} else
emit renderedPage(num, image);
}
/*
* Don't worry about handling errors in the getters; anything interesting
* would have happened earlier in load(), or will happen later in render().
* These are expected to always just return something sensible.
*/
int PDFRenderer::numPages() const
{
return (data->document == nullptr) ? 0 : data->document->numPages();
}
QSize PDFRenderer::pageSize(int num) const
{
if (pageExists(num)) {
std::unique_ptr<Poppler::Page> page = data->document->page(num);
if (page != nullptr) {
QSize pointSize = page->pageSize();
// Convert points to pixels at our current DPI
return zoomScaled(QSize(pointSize.width() * dpiX() / 72,
pointSize.height() * dpiY() / 72));
}
}
return QSize(0, 0);
}
bool PDFRenderer::loadFromData(const QByteArray &bytes)
{
QMutexLocker locker(&popplerErrorMutex);
popplerError.clear();
data->document = Poppler::Document::loadFromData(bytes);
if (data->document == nullptr) {
storeLoadError(popplerError);
popplerError.clear();
return false;
} else
return true;
}
/*
* Stores debug and error messages from Poppler so we can display them
* in the application.
* Note the mutex has already been locked by the time we get here.
*/
void storePopplerError(const QString &message, const QVariant &closure)
{
(void)closure;
// Do not clear popplerError here; any given Poppler method call may
// result in more than one error, and we want to preserve them all
if (!popplerError.isEmpty())
popplerError += "\n";
popplerError += message;
}