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