-
Notifications
You must be signed in to change notification settings - Fork 29
/
pdfdocumentreference.cpp
95 lines (85 loc) · 3.01 KB
/
pdfdocumentreference.cpp
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
#include "pdfdocumentreference.h"
#include "pdfpagereference.h"
#include <stdexcept>
#include <QFile>
#include "debug.h"
QSharedPointer< const Poppler::Document > PDFDocumentReference::popplerDocument() const
{
// Make sure this function is "single threaded"
Lock lk{mutex_};
if ( ! popplerDocument_ ) {
/** No document defined yet. Create it from the disk/memory cache. */
QSharedPointer<Poppler::Document> m_document;
if ( cacheOption() == PDFCacheOption::rereadFromDisk ) {
DEBUGOUT << "Trying to build a Poppler::Document from file" << filename();
QSharedPointer<Poppler::Document> diskDocument( Poppler::Document::load(filename()) );
m_document.swap(diskDocument);
}
else if ( cacheOption() == PDFCacheOption::keepPDFinMemory ) {
DEBUGOUT << "Trying to build a document from" << fileContents_.size() << "byte memory cache";
QSharedPointer<Poppler::Document> memoryDocument( Poppler::Document::loadFromData(fileContents_) );
m_document.swap(memoryDocument);
}
if ( !m_document || m_document->isLocked() )
throw std::runtime_error("Document not readable");
m_document->setRenderHint(Poppler::Document::Antialiasing, true);
m_document->setRenderHint(Poppler::Document::TextAntialiasing, true);
m_document->setRenderHint(Poppler::Document::TextHinting, true);
popplerDocument_ = m_document;
}
return popplerDocument_;
}
PDFPageReference PDFDocumentReference::page(unsigned int pageNumber) const
{
PDFPageReference ppr(*this, pageNumber);
return ppr;
}
PDFDocumentReference::PDFDocumentReference(const QString& theFilename, const PDFCacheOption& theCacheOption):
filename_(theFilename),
fileContents_(),
cacheOption_(theCacheOption),
mutex_(),
popplerDocument_()
{
if ( cacheOption() == PDFCacheOption::keepPDFinMemory ) {
DEBUGOUT << "Reading file into memory";
QFile file(filename());
file.open(QIODevice::ReadOnly);
fileContents_ = file.readAll();
DEBUGOUT << fileContents_.size() << "bytes read";
}
}
const PDFCacheOption& PDFDocumentReference::cacheOption() const
{
return cacheOption_;
}
const QString& PDFDocumentReference::filename() const
{
return filename_;
}
PDFDocumentReference& PDFDocumentReference::operator=(const PDFDocumentReference& rhs)
{
if ( rhs.filename() != filename() ) {
throw std::runtime_error("This PDFDocumentReference has a different filename");
}
if ( rhs.cacheOption() != cacheOption() ) {
throw std::runtime_error("This PDFDocumentReference has a different cache setting");
}
fileContents_=rhs.fileContents_;
popplerDocument_=rhs.popplerDocument_;
return *this;
}
bool operator==(const PDFDocumentReference& lhs, const PDFDocumentReference& rhs)
{
if ( lhs.cacheOption() != rhs.cacheOption() ) {
return false;
}
else if ( lhs.cacheOption() == PDFCacheOption::keepPDFinMemory ) {
DEBUGOUT << "Using memory cache, comparing byte-by-byte.";
return lhs.fileContents_ == rhs.fileContents_;
}
else {
DEBUGOUT << "Not using memory cache, just comparing the filename.";
return lhs.filename() == rhs.filename();
}
}