]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PDFDoc.cc
Compiles at least :)
[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(GString *fileName1) {
33   FileStream *str;
34   Object catObj;
35   Object obj;
36   GString *fileName2;
37
38   // setup
39   ok = gFalse;
40   catalog = NULL;
41   xref = NULL;
42   file = NULL;
43   links = NULL;
44
45   // try to open file
46   fileName = fileName1;
47   fileName2 = NULL;
48 #ifdef VMS
49   if (!(file = fopen(fileName->getCString(), "rb", "ctx=stm"))) {
50     error(-1, "Couldn't open file '%s'", fileName->getCString());
51     return;
52   }
53 #else
54   if (!(file = fopen(fileName->getCString(), "rb"))) {
55     fileName2 = fileName->copy();
56     fileName2->lowerCase();
57     if (!(file = fopen(fileName2->getCString(), "rb"))) {
58       fileName2->upperCase();
59       if (!(file = fopen(fileName2->getCString(), "rb"))) {
60         error(-1, "Couldn't open file '%s'", fileName->getCString());
61         delete fileName2;
62         return;
63       }
64     }
65     delete fileName2;
66   }
67 #endif
68
69   // create stream
70   obj.initNull();
71   str = new FileStream(file, 0, -1, &obj);
72
73   // check header
74   str->checkHeader();
75
76   // read xref table
77   xref = new XRef(str);
78   delete str;
79   if (!xref->isOk()) {
80     error(-1, "Couldn't read xref table");
81     return;
82   }
83
84   // read catalog
85   catalog = new Catalog(xref->getCatalog(&catObj));
86   catObj.free();
87   if (!catalog->isOk()) {
88     error(-1, "Couldn't read page catalog");
89     return;
90   }
91
92   // done
93   ok = gTrue;
94   return;
95 }
96
97 PDFDoc::~PDFDoc() {
98   if (catalog)
99     delete catalog;
100   if (xref)
101     delete xref;
102   if (file)
103     fclose(file);
104   if (fileName)
105     delete fileName;
106   if (links)
107     delete links;
108 }
109
110 void PDFDoc::displayPage(OutputDev *out, int page, int zoom, int rotate,
111                          GBool doLinks) {
112   Link *link;
113   double x1, y1, x2, y2;
114   double w;
115   int i;
116
117   if (printCommands)
118     printf("***** page %d *****\n", page);
119   catalog->getPage(page)->display(out, zoom, rotate);
120   if (doLinks) {
121     if (links)
122       delete links;
123     getLinks(page);
124     for (i = 0; i < links->getNumLinks(); ++i) {
125       link = links->getLink(i);
126       link->getBorder(&x1, &y1, &x2, &y2, &w);
127       if (w > 0)
128         out->drawLinkBorder(x1, y1, x2, y2, w);
129     }
130     out->dump();
131   }
132 }
133
134 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
135                           int zoom, int rotate) {
136   Page *p;
137   int page;
138
139   for (page = firstPage; page <= lastPage; ++page) {
140     if (printCommands)
141       printf("***** page %d *****\n", page);
142     p = catalog->getPage(page);
143     p->display(out, zoom, rotate);
144   }
145 }
146
147 GBool PDFDoc::saveAs(GString *name) {
148   FILE *f;
149   char buf[4096];
150   int n;
151
152   if (!(f = fopen(name->getCString(), "wb"))) {
153     error(-1, "Couldn't open file '%s'", name->getCString());
154     return gFalse;
155   }
156   rewind(file);
157   while ((n = fread(buf, 1, sizeof(buf), file)) > 0)
158     fwrite(buf, 1, n, f);
159   fclose(f);
160   return gTrue;
161 }
162
163 void PDFDoc::getLinks(int page) {
164   Object obj;
165
166   links = new Links(catalog->getPage(page)->getAnnots(&obj),
167                     catalog->getBaseURI());
168   obj.free();
169 }