]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PDFDoc.cc
embryonic PDF viewer as a Bonobo container...
[evince.git] / pdf / xpdf / PDFDoc.cc
1 //========================================================================
2 //
3 // PDFDoc.cc
4 //
5 // Copyright 1996 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #ifdef __GNUC__
10 #pragma implementation
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include "GString.h"
18 #include "config.h"
19 #include "Page.h"
20 #include "Catalog.h"
21 #include "XRef.h"
22 #include "Link.h"
23 #include "OutputDev.h"
24 #include "Params.h"
25 #include "Error.h"
26 #include "PDFDoc.h"
27
28 //------------------------------------------------------------------------
29 // PDFDoc
30 //------------------------------------------------------------------------
31
32 PDFDoc::PDFDoc(BaseFile file1, GString *fileName1) {
33   FileStream *str;
34   Object catObj;
35   Object obj;
36
37   // setup
38   ok = gFalse;
39   catalog = NULL;
40   xref = NULL;
41   file = NULL;
42   links = NULL;
43
44   fileName = fileName1;
45   file = file1;
46   if (!file)
47     return;
48
49   // create stream
50   obj.initNull();
51   str  = new FileStream(file, 0, -1, &obj);
52
53   // check header
54   str->checkHeader();
55
56   // read xref table
57   xref = new XRef(str);
58   delete str;
59   if (!xref->isOk()) {
60     error(-1, "Couldn't read xref table");
61     return;
62   }
63
64   // read catalog
65   catalog = new Catalog(xref->getCatalog(&catObj));
66   catObj.free();
67   if (!catalog->isOk()) {
68     error(-1, "Couldn't read page catalog");
69     return;
70   }
71
72   // done
73   ok = gTrue;
74   return;
75 }
76
77 PDFDoc::~PDFDoc() {
78   if (catalog)
79     delete catalog;
80   if (xref)
81     delete xref;
82   if (file)
83     bfclose(file);
84   if (fileName)
85     delete fileName;
86   if (links)
87     delete links;
88 }
89
90 void PDFDoc::displayPage(OutputDev *out, int page, int zoom, int rotate,
91                          GBool doLinks) {
92   Link *link;
93   double x1, y1, x2, y2;
94   double w;
95   int i;
96
97   if (printCommands)
98     printf("***** page %d *****\n", page);
99   catalog->getPage(page)->display(out, zoom, rotate);
100   if (doLinks) {
101     if (links)
102       delete links;
103     getLinks(page);
104     for (i = 0; i < links->getNumLinks(); ++i) {
105       link = links->getLink(i);
106       link->getBorder(&x1, &y1, &x2, &y2, &w);
107       if (w > 0)
108         out->drawLinkBorder(x1, y1, x2, y2, w);
109     }
110     out->dump();
111   }
112 }
113
114 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
115                           int zoom, int rotate) {
116   Page *p;
117   int page;
118
119   for (page = firstPage; page <= lastPage; ++page) {
120     if (printCommands)
121       printf("***** page %d *****\n", page);
122     p = catalog->getPage(page);
123     p->display(out, zoom, rotate);
124   }
125 }
126
127 GBool PDFDoc::saveAs(GString *name) {
128   FILE *f;
129   char buf[4096];
130   int n;
131
132   if (!(f = fopen(name->getCString(), "wb"))) {
133     error(-1, "Couldn't open file '%s'", name->getCString());
134     return gFalse;
135   }
136   brewind(file);
137   while ((n = bfread(buf, 1, sizeof(buf), file)) > 0)
138     fwrite(buf, 1, n, f);
139   fclose(f);
140   return gTrue;
141 }
142
143 void PDFDoc::getLinks(int page) {
144   Object obj;
145
146   links = new Links(catalog->getPage(page)->getAnnots(&obj),
147                     catalog->getBaseURI());
148   obj.free();
149 }