mupdf-qt  0.1
Qt5 interface of the popular PDF library MuPDF
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Pages
mupdf-document.cpp
Go to the documentation of this file.
1 #include "mupdf-document.h"
2 #include "mupdf-document_p.h"
3 #include "mupdf-page.h"
4 #include "mupdf-page_p.h"
5 #include "mupdf-outline.h"
6 #include "mupdf-outline_p.h"
7 extern "C" {
8 #include <mupdf/fitz.h>
9 }
10 #include <QString>
11 #include <QDateTime>
12 
13 namespace MuPDF
14 {
15 
23 Document * loadDocument(const QString &filePath)
24 {
25  DocumentPrivate *documentp;
26  Document *document;
27 
28  // Create DocumentPrivate
29  documentp = new DocumentPrivate(filePath);
30  if (!documentp)
31  return NULL;
32  else if (!documentp->context || !documentp->document) {
33  delete documentp;
34  return NULL;
35  }
36 
37  // Create Document
38  document = new Document(documentp);
39  return document;
40 }
41 
49 Document * loadDocument(const QByteArray &bytes)
50 {
51  DocumentPrivate *documentp;
52  Document *document;
53 
54  // Create DocumentPrivate
55  documentp = new DocumentPrivate((unsigned char *)bytes.data(), bytes.length());
56  if (!documentp)
57  return NULL;
58  else if (!documentp->context || !documentp->document) {
59  delete documentp;
60  return NULL;
61  }
62 
63  // Create Document
64  document = new Document(documentp);
65  return document;
66 }
67 
68 DocumentPrivate::DocumentPrivate(const QString &filePath)
69  : context(NULL), document(NULL)
70  , transparent(false)
71  , b(-1), g(-1), r(-1), a(-1)
72 {
73  // create context
74  context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
75  if (!context)
76  return;
77 
78  // register the default file types
79  fz_register_document_handlers(context);
80 
81  // open document
82  fz_try(context)
83  {
84  document = fz_open_document(context, filePath.toUtf8().data());
85  }
86  fz_catch(context)
87  {
88  deleteData();
89  }
90 }
91 
92 DocumentPrivate::DocumentPrivate(unsigned char *bytes, int len)
93  : context(NULL), document(NULL)
94  , transparent(false)
95  , b(-1), g(-1), r(-1), a(-1)
96 {
97  // create context
98  context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
99  if (!context)
100  return;
101 
102  // register the default file types
103  fz_register_document_handlers(context);
104 
105  // open document
106  fz_try(context)
107  {
108  // access to the bytes stream
109  fz_stream* stream = fz_open_memory(context, bytes, len);
110 
111  document = fz_open_document_with_stream(context, ".pdf", stream);
112  }
113  fz_catch(context)
114  {
115  deleteData();
116  }
117 }
118 
122 Document::~Document()
123 {
124  delete d;
125  d = NULL;
126 }
127 
131 bool Document::needsPassword() const
132 {
133  return fz_needs_password(d->document);
134 }
135 
145 bool Document::authPassword(const QString &password)
146 {
147  return fz_authenticate_password(d->document,
148  password.toLocal8Bit().data());
149 }
150 
154 int Document::numPages() const
155 {
156  int ret;
157  fz_try(d->context)
158  {
159  ret = fz_count_pages(d->document);
160  }
161  fz_catch(d->context)
162  {
163  ret = -1;
164  }
165  return ret;
166 }
167 
175 Page * Document::page(int index) const
176 {
177  PagePrivate *pagep;
178  Page *page;
179 
180  // Create PagePrivate
181  pagep = new PagePrivate(d, index);
182  if (!pagep)
183  return NULL;
184  else if (!pagep->page) {
185  delete pagep;
186  return NULL;
187  }
188 
189  // Create Page
190  page = new Page(pagep);
191  if (page)
192  d->pages << pagep;
193  return page;
194 }
195 
203 Outline * Document::outline() const
204 {
205  fz_outline *o;
206  OutlinePrivate *outlinep;
207 
208  o = fz_load_outline(d->document);
209  if (o) {
210  outlinep = new OutlinePrivate(d, o);
211  d->outlines << outlinep;
212  return new Outline(outlinep);
213  }
214 
215  return NULL;
216 }
217 
221 QString Document::pdfVersion() const
222 {
223  pdf_document *xref = (pdf_document *)d->document;
224  return QString::number(xref->version / 10.0f);
225 }
226 
230 QString Document::title() const
231 {
232  return d->info("Title");
233 }
234 
238 QString Document::author() const
239 {
240  return d->info("Author");
241 }
242 
246 QString Document::subject() const
247 {
248  return d->info("Subject");
249 }
250 
254 QString Document::keywords() const
255 {
256  return d->info("Keywords");
257 }
258 
262 QString Document::creator() const
263 {
264  return d->info("Creator");
265 }
266 
270 QString Document::producer() const
271 {
272  return d->info("Producer");
273 }
274 
278 QDateTime Document::creationDate() const
279 {
280  QString str = d->info("CreationDate");
281  if (str.isEmpty()) {
282  return QDateTime();
283  }
284  // see pdf_reference_1.7.pdf 2.8.3 Dates
285  return QDateTime::fromString(str.left(16),
286  "'D:'yyyyMMddHHmmss");
287 }
288 
292 QDateTime Document::modDate() const
293 {
294  QString str = d->info("ModDate");
295  if (str.isEmpty()) {
296  return QDateTime();
297  }
298  // see pdf_reference_1.7.pdf 2.8.3 Dates
299  return QDateTime::fromString(str.left(16),
300  "'D:'yyyyMMddHHmmss");
301 }
302 
310 void Document::setTransparentRendering(bool enable)
311 {
312  d->transparent = enable;
313 }
314 
329 void Document::setBackgroundColor(int r, int g, int b, int a)
330 {
331  d->r = r;
332  d->g = g;
333  d->b = b;
334  d->a = a;
335 }
336 
337 DocumentPrivate::~DocumentPrivate()
338 {
339  foreach (OutlinePrivate *outlinep, outlines) {
340  outlinep->deleteData();
341  }
342  foreach (PagePrivate *pagep, pages) {
343  pagep->deleteData();
344  }
345 
346  deleteData();
347 }
348 
349 } // end namespace MuPDF