]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdf-document.cc
9a9d022e5f2974a3f1782ec1a4528180611293cf
[evince.git] / pdf / xpdf / pdf-document.cc
1 /* pdfdocument.h: Implementation of EvDocument for PDF
2  * Copyright (C) 2004, Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include "gpdf-g-switch.h"
20 #include "pdf-document.h"
21 #include "gpdf-g-switch.h"
22
23 #include "GlobalParams.h"
24 #include "GDKSplashOutputDev.h"
25 #include "PDFDoc.h"
26
27 typedef struct _PdfDocumentClass PdfDocumentClass;
28
29 #define PDF_DOCUMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
30 #define PDF_IS_DOCUMENT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
31 #define PDF_DOCUMENT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))
32
33 struct _PdfDocumentClass
34 {
35         GObjectClass parent_class;
36 };
37
38 struct _PdfDocument
39 {
40         GObject parent_instance;
41
42         int page;
43         int page_x_offset;
44         int page_y_offset;
45         double scale;
46         GdkDrawable *target;
47
48         GDKSplashOutputDev *out;
49         PDFDoc *doc;
50
51         gboolean page_valid;
52 };
53
54 static void pdf_document_document_iface_init (EvDocumentIface *iface);
55
56 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
57                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
58                                                   pdf_document_document_iface_init) });
59
60 static gboolean
61 document_validate_page (PdfDocument *pdf_document)
62 {
63         if (!pdf_document->page_valid) {
64                 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
65                                                 72 * pdf_document->scale,
66                                                 72 * pdf_document->scale,
67                                                 0, gTrue, gTrue);
68                 
69                 pdf_document->page_valid = TRUE;
70         }
71 }
72
73 static gboolean
74 pdf_document_load (EvDocument  *document,
75                    const char  *uri,
76                    GError     **error)
77 {
78         PdfDocument *pdf_document = PDF_DOCUMENT (document);
79         PDFDoc *newDoc;
80         int err;
81         char *filename;
82         GString *filename_g;
83         
84         if (!globalParams) {
85                 globalParams = new GlobalParams("/etc/xpdfrc");
86                 globalParams->setupBaseFonts(NULL);
87         }
88
89         filename = g_filename_from_uri (uri, NULL, error);
90         if (!filename)
91                 return FALSE;
92
93         filename_g = new GString (filename);
94         g_free (filename);
95
96         // open the PDF file, assumes ownership of filename_g
97         newDoc = new PDFDoc(filename_g, 0, 0);
98
99         if (!newDoc->isOk()) {
100                 err = newDoc->getErrorCode();
101                 delete newDoc;
102
103                 /* FIXME: Add a real error enum to EvDocument */
104                 g_set_error (error, G_FILE_ERROR,
105                              G_FILE_ERROR_FAILED,
106                              "Failed to load document (error %d) '%s'\n",
107                              err,
108                              uri);
109                 
110                 return FALSE;
111         }
112
113         if (pdf_document->doc)
114                 delete pdf_document->doc;
115         pdf_document->doc = newDoc;
116
117         pdf_document->page = 1;
118
119         if (pdf_document->out)
120                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
121
122         pdf_document->page_valid = FALSE;
123         
124         return TRUE;
125 }
126
127 static int
128 pdf_document_get_n_pages (EvDocument  *document)
129 {
130         PdfDocument *pdf_document = PDF_DOCUMENT (document);
131
132         if (pdf_document->doc)
133                 return pdf_document->doc->getNumPages();
134         else
135                 return 1;
136 }
137
138 static void
139 pdf_document_set_page (EvDocument  *document,
140                        int          page)
141 {
142         PdfDocument *pdf_document = PDF_DOCUMENT (document);
143
144         if (page != pdf_document->page) {
145                 pdf_document->page = page;
146                 pdf_document->page_valid = FALSE;
147         }
148
149 }
150
151 static void
152 redraw_callback (void *data)
153 {
154         /* Need to hook up through a EvDocument callback? */
155 }
156
157 static void
158 pdf_document_set_target (EvDocument  *document,
159                          GdkDrawable *target)
160 {
161         PdfDocument *pdf_document = PDF_DOCUMENT (document);
162         
163         if (pdf_document->target != target) {
164                 if (pdf_document->target)
165                         g_object_unref (pdf_document->target);
166                 
167                 pdf_document->target = target;
168
169                 if (pdf_document->target)
170                         g_object_ref (pdf_document->target);
171
172                 if (pdf_document->out) {
173                         delete pdf_document->out;
174                         pdf_document->out = NULL;
175                 }
176
177                 if (pdf_document->target) {
178                         pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
179                                                          redraw_callback, (void*) document);
180
181                         if (pdf_document->doc)
182                                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
183
184                 }
185
186                 pdf_document->page_valid = FALSE;
187         }
188 }
189
190 static void
191 pdf_document_set_scale (EvDocument  *document,
192                         double       scale)
193 {
194         PdfDocument *pdf_document = PDF_DOCUMENT (document);
195         
196         if (pdf_document->scale != scale) {
197                 pdf_document->scale = scale;
198                 pdf_document->page_valid = FALSE;
199         }
200 }
201
202 static void
203 pdf_document_set_page_offset (EvDocument  *document,
204                               int          x,
205                               int          y)
206 {
207         PdfDocument *pdf_document = PDF_DOCUMENT (document);
208         
209         pdf_document->page_x_offset = x;
210         pdf_document->page_y_offset = y;
211 }
212
213 static void
214 pdf_document_get_page_size (EvDocument   *document,
215                             int          *width,
216                             int          *height)
217 {
218         PdfDocument *pdf_document = PDF_DOCUMENT (document);
219
220         if (document_validate_page (pdf_document)) {
221                 if (width)
222                         *width = pdf_document->out->getBitmapWidth();
223                 if (height)
224                         *height = pdf_document->out->getBitmapHeight();
225         } else {
226                 if (width)
227                         *width = 1;
228                 if (height)
229                         *height = 1;
230         }
231 }
232
233 static void
234 pdf_document_render (EvDocument  *document,
235                      int          clip_x,
236                      int          clip_y,
237                      int          clip_width,
238                      int          clip_height)
239 {
240         PdfDocument *pdf_document = PDF_DOCUMENT (document);
241         GdkRectangle page;
242         GdkRectangle draw;
243
244         if (!document_validate_page (pdf_document) || !pdf_document->target)
245                 return;
246         
247         page.x = pdf_document->page_x_offset;
248         page.y = pdf_document->page_y_offset;
249         page.width = pdf_document->out->getBitmapWidth();
250         page.height = pdf_document->out->getBitmapHeight();
251
252         draw.x = clip_x;
253         draw.y = clip_y;
254         draw.width = clip_width;
255         draw.height = clip_height;
256         
257         if (gdk_rectangle_intersect (&page, &draw, &draw))
258                 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
259                                            pdf_document->target,
260                                            draw.x, draw.y,
261                                            draw.width, draw.height);
262 }
263
264 static void
265 pdf_document_finalize (GObject *object)
266 {
267         PdfDocument *pdf_document = PDF_DOCUMENT (object);
268
269         if (pdf_document->target)
270                 g_object_unref (pdf_document->target);
271
272         if (pdf_document->out)
273                 delete pdf_document->out;
274         if (pdf_document->doc)
275                 delete pdf_document->doc;
276
277 }
278
279 static void
280 pdf_document_class_init (PdfDocumentClass *klass)
281 {
282         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
283   
284         gobject_class->finalize = pdf_document_finalize;
285 }
286
287 static void
288 pdf_document_document_iface_init (EvDocumentIface *iface)
289 {
290         iface->load = pdf_document_load;
291         iface->get_n_pages = pdf_document_get_n_pages;
292         iface->set_page = pdf_document_set_page;
293         iface->set_scale = pdf_document_set_scale;
294         iface->set_target = pdf_document_set_target;
295         iface->set_page_offset = pdf_document_set_page_offset;
296         iface->get_page_size = pdf_document_get_page_size;
297         iface->render = pdf_document_render;
298 }
299
300 static void
301 pdf_document_init (PdfDocument *pdf_document)
302 {
303         pdf_document->page = 1;
304         pdf_document->page_x_offset = 0;
305         pdf_document->page_y_offset = 0;
306         pdf_document->scale = 1.;
307         
308         pdf_document->page_valid = FALSE;
309 }
310