[go: up one dir, main page]

Menu

[58c4ad]: / src / pdfpager.cpp  Maximize  Restore  History

Download this file

227 lines (208 with data), 6.3 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/****************************************************************************
** This file is part of the taborca project hosted at
** sf.net/projects/taborca
** Copyright (C) 2013 Shawn Rutledge
** Contact: s@ecloud.org
**
** 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 in a file called LICENSE; if not, write to the
** Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
****************************************************************************/
#include "pdfpager.h"
#include "common.h"
#include <QThreadPool>
PDFPager::PDFPager() :
pdf(NULL),
pageRange(this),
m_pageNum(-1),
m_renderSize(600, 600),
m_thumbnailRenderSize(72, 72)
{
setAutoDelete(false);
}
void PDFPager::openPDF(QString fpath)
{
m_path = fpath;
m_nullThumbnails.clear();
pdf = Poppler::Document::load(fpath);
if (!pdf || pdf->isLocked())
{
/// @todo ... error message ....
if (pdf)
delete pdf;
pdf = NULL;
return;
}
qDebug("open pdf success");
int pages = pdf->numPages();
m_cache.resize(pages);
emit opened(fpath, pages);
emit numPages(pages);
for (int p = 0; p < pages; ++p)
{
Poppler::Page* pdfPage = pdf->page(p);
if (pdfPage)
emit thumbnail(p, pdfPage->thumbnail(), pdfPage->label());
if (pdfPage)
{
QImage thumbnail = pdfPage->thumbnail();
if (thumbnail.isNull())
{
qDebug("null thumbnail for page %d", p);
m_nullThumbnails.append(p);
}
// Doesn't work. WTF...
// emit thumbnail(p, pdfPage->thumbnail(), pdfPage->label());
}
}
page(0);
pageRange.setRange(0, pages - 1);
pdf->setRenderHint(Poppler::Document::Antialiasing);
pdf->setRenderHint(Poppler::Document::TextAntialiasing);
pdf->setRenderHint(Poppler::Document::TextHinting);
// Start rendering thumbnails (if necessary) and pages in the background
QThreadPool::globalInstance()->start(this, QThread::LowPriority);
}
void PDFPager::closePDF()
{
if (!m_path.isEmpty() && pdf)
{
emit closed(m_path);
delete pdf;
pdf = NULL;
page(-1);
}
}
void PDFPager::page(int pnum)
{
if (pnum < 0)
return;
if (pnum >= m_cache.size())
return;
m_pageNum = pnum;
// Page fromCache = m_cache[pnum];
// if (fromCache.isNull())
render(pnum);
// else
// {
// emit rendered(pnum, fromCache);
// emit pageText(fromCache.textStrings);
// emit pageChanged(pnum);
// emit pageChanged(fromCache.label);
// }
}
void PDFPager::nextPage(bool forward)
{
if (forward)
page(m_pageNum + 1);
else
page(m_pageNum - 1);
}
void PDFPager::renderSize(QSize s, bool reRender)
{
qDebug("renderSize(%d x %d)", s.width(), s.height());
m_renderSize = s;
if (reRender)
page(m_pageNum);
}
void PDFPager::render(int pnum, QSize renderSize, bool emitImage)
{
m_renderMutex.lock();
qDebug("render(%d, %d x %d)", pnum, renderSize.width(), renderSize.height());
if (pnum >= pdf->numPages())
return;
if (!renderSize.isValid())
renderSize = m_renderSize;
emit rendering(true);
// Access page of the PDF file
if (!pdf)
return;
Poppler::Page* pdfPage = pdf->page(pnum); // Document starts at page 0
if (!pdfPage)
{
// ... error message ...
emit rendering(false);
return;
}
// Generate a QImage of the rendered page
double res = MIN(((double)(renderSize.width())) / // dots
(pdfPage->pageSizeF().width() / 72.0), // inches
((double)(renderSize.height())) / // dots
(pdfPage->pageSizeF().height() / 72.0)); // inches
qDebug("res would be %lf\n", res);
//qDebug("renderToImage: res %f to fit %d x %d", res, renderSize.width(), renderSize.height());
// @hack For now, fix the res at 72, because that way 1 point = 1 model unit,
// which helps the text items to match the image.
/// @todo define model units in terms of points:
/// on load, choose an appropriate resolution and
/// then convert TextBox point units to model units.
// if (res > 8) // e.g. it's not a thumbnail
// res = 144; // this will break text items matchup
// res = 72.0;
QImage image = pdfPage->renderToImage(res, res);//, 0, 0, 1000, 1000);
if (image.isNull())
{
// ... error message ...
emit rendering(false);
return;
}
//qDebug("renderDone: %d x %d", image.width(), image.height());
QString label = pdfPage->label();
if (label.isEmpty())
label = QString::number(pnum);
Page pcache = Page(label, image, pdfPage->textList());
if (res < 8) // a thumbnail
m_cache[pnum] = pcache;
if (emitImage)
{
emit pageChanged(pnum);
emit pageChanged(label);
renderDone(pnum, image);
emit pageText(pcache.textStrings);
}
// after reading everything of interest, the page can be deleted
// (pdf->page(pnum) resulted in ownership of that copy of the data,
// so this function is responsible to delete it)
delete pdfPage;
m_renderMutex.unlock();
}
void PDFPager::renderDone(int pnum, QImage pm)
{
emit rendered(pnum, pm);
emit rendering(false);
}
void PDFPager::run()
{
qDebug("PDFPager::run()");
int pages = pdf->numPages();
for (int i = 0; i < pages; ++i)
{
if (m_nullThumbnails.contains(i))
{
render(i, m_thumbnailRenderSize, false);
// @hack The cache happens to have a thumbnail right now,
// if no other rendering happened between the line above and this one.
qDebug("for page %d, emitting thumbnail with width %d; requested size was %d", i, m_cache[i].width(), m_thumbnailRenderSize.width());
emit thumbnail(i, m_cache[i], QString::number(i));
}
emit statusMessage(QString("pre-rendering page %1").arg(i));
// Put the full-size image in the cache
if (m_pageNum - 10 < i && i < m_pageNum + 10)
{
qDebug("pre-rendering page %d, near current page %d", i, m_pageNum);
render(i, m_renderSize, false);
}
}
emit statusClear();
}