File: pdfdocumentreference.cpp

package info (click to toggle)
dspdfviewer 1.15.1%2Bgit20230427.d432d8d-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 924 kB
  • sloc: cpp: 2,303; makefile: 22; sh: 7
file content (95 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download | duplicates (4)
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
#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();
  }
}