forked from dannyedel/dspdfviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfdocumentreference.cpp
82 lines (73 loc) · 2.59 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
#include "pdfdocumentreference.h"
#include "pdfpagereference.h"
#include <stdexcept>
#include <QFile>
#include "debug.h"
QSharedPointer< Poppler::Document > PDFDocumentReference::popplerDocument() const
{
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);
return m_document;
}
PDFPageReference PDFDocumentReference::page(unsigned int pageNumber) const
{
PDFPageReference ppr(*this, pageNumber);
return ppr;
}
PDFDocumentReference::PDFDocumentReference(const QString& theFilename, const PDFCacheOption& theCacheOption):
filename_(theFilename),
cacheOption_(theCacheOption)
{
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_;
return *this;
}
bool operator==(const PDFDocumentReference& lhs, const PDFDocumentReference& rhs)
{
if ( lhs.cacheOption() != rhs.cacheOption() ) {
return false;
}
else if ( lhs.cacheOption() == PDFCacheOption::keepPDFinMemory ) {
return lhs.fileContents_ == rhs.fileContents_;
}
else {
return lhs.filename() == rhs.filename();
}
}