]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdf-document.cc
PDFDoc constructor assumes ownership of the string passed in.
[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         GdkRectangle page_rect;
44         GdkDrawable *target;
45
46         GDKSplashOutputDev *out;
47         PDFDoc *doc;
48         
49 };
50
51 static void pdf_document_document_iface_init (EvDocumentIface *iface);
52
53 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
54                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
55                                                   pdf_document_document_iface_init) });
56
57
58 static gboolean
59 pdf_document_load (EvDocument  *document,
60                    const char  *uri,
61                    GError     **error)
62 {
63         PdfDocument *pdf_document = PDF_DOCUMENT (document);
64         PDFDoc *newDoc;
65         int err;
66         char *filename;
67         GString *filename_g;
68         
69         if (!globalParams) {
70                 globalParams = new GlobalParams("/etc/xpdfrc");
71                 globalParams->setupBaseFonts(NULL);
72         }
73
74         filename = g_filename_from_uri (uri, NULL, error);
75         if (!filename)
76                 return FALSE;
77
78         filename_g = new GString (filename);
79         g_free (filename);
80
81         // open the PDF file, assumes ownership of filename_g
82         newDoc = new PDFDoc(filename_g, 0, 0);
83
84         if (!newDoc->isOk()) {
85                 err = newDoc->getErrorCode();
86                 delete newDoc;
87
88                 /* FIXME: Add a real error enum to EvDocument */
89                 g_set_error (error, G_FILE_ERROR,
90                              G_FILE_ERROR_FAILED,
91                              "Failed to load document (error %d) '%s'\n",
92                              err,
93                              uri);
94                 
95                 return FALSE;
96         }
97
98         if (pdf_document->doc)
99                 delete pdf_document->doc;
100         pdf_document->doc = newDoc;
101
102         if (pdf_document->out) {
103                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
104                 pdf_document->doc->displayPage (pdf_document->out, 1, 72, 72, 0, gTrue, gTrue);
105         }
106         
107         return TRUE;
108 }
109
110 static int
111 pdf_document_get_n_pages (EvDocument  *document)
112 {
113         PdfDocument *pdf_document = PDF_DOCUMENT (document);
114
115         if (pdf_document->doc)
116                 return pdf_document->doc->getNumPages();
117         else
118                 return 1;
119 }
120
121 static void
122 pdf_document_set_page (EvDocument  *document,
123                        int          page)
124 {
125         PdfDocument *pdf_document = PDF_DOCUMENT (document);
126
127         pdf_document->page = page;
128 }
129
130 static void
131 redraw_callback (void *data)
132 {
133         /* Need to hook up through a EvDocument callback? */
134 }
135
136 static void
137 pdf_document_set_target (EvDocument  *document,
138                          GdkDrawable *target)
139 {
140         PdfDocument *pdf_document = PDF_DOCUMENT (document);
141         
142         if (pdf_document->target != target) {
143                 if (pdf_document->target)
144                         g_object_unref (pdf_document->target);
145                 
146                 pdf_document->target = target;
147
148                 if (pdf_document->target)
149                         g_object_ref (pdf_document->target);
150
151                 if (pdf_document->out)
152                         delete pdf_document->out;
153
154                 if (pdf_document->target) {
155                         pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
156                                                          redraw_callback, (void*) document);
157
158                         if (pdf_document->doc) {
159                                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
160                                 pdf_document->doc->displayPage (pdf_document->out, 1, 72, 72, 0, gTrue, gTrue);
161                         }
162                 }
163         }
164 }
165
166 static void
167 pdf_document_set_page_rect (EvDocument  *document,
168                             int          x,
169                             int          y,
170                             int          width,
171                             int          height)
172 {
173         PdfDocument *pdf_document = PDF_DOCUMENT (document);
174         
175         pdf_document->page_rect.x = x;
176         pdf_document->page_rect.y = y;
177         pdf_document->page_rect.width = width;
178         pdf_document->page_rect.height = height;
179 }
180
181 static void
182 pdf_document_render (EvDocument  *document,
183                      int          clip_x,
184                      int          clip_y,
185                      int          clip_width,
186                      int          clip_height)
187 {
188         PdfDocument *pdf_document = PDF_DOCUMENT (document);
189         GdkRectangle page;
190         GdkRectangle draw;
191
192         if (!pdf_document->target)
193                 return;
194         
195         page.x = 0;
196         page.y = 0;
197         page.width = pdf_document->out->getBitmapWidth();
198         page.height = pdf_document->out->getBitmapHeight();
199
200         draw.x = clip_x;
201         draw.y = clip_y;
202         draw.width = clip_width;
203         draw.height = clip_height;
204         
205         if (gdk_rectangle_intersect (&page, &draw, &draw))
206                 pdf_document->out->redraw (draw.x, draw.y,
207                                            pdf_document->target,
208                                            draw.x, draw.y,
209                                            draw.width, draw.height);
210 }
211
212 static void
213 pdf_document_finalize (GObject *object)
214 {
215         PdfDocument *pdf_document = PDF_DOCUMENT (object);
216
217         if (pdf_document->target)
218                 g_object_unref (pdf_document->target);
219
220         if (pdf_document->out)
221                 delete pdf_document->out;
222         if (pdf_document->doc)
223                 delete pdf_document->doc;
224
225 }
226
227 static void
228 pdf_document_class_init (PdfDocumentClass *klass)
229 {
230         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231   
232         gobject_class->finalize = pdf_document_finalize;
233 }
234
235 static void
236 pdf_document_document_iface_init (EvDocumentIface *iface)
237 {
238         iface->load = pdf_document_load;
239         iface->get_n_pages = pdf_document_get_n_pages;
240         iface->set_page = pdf_document_set_page;
241         iface->set_target = pdf_document_set_target;
242         iface->set_page_rect = pdf_document_set_page_rect;
243         iface->render = pdf_document_render;
244 }
245
246 static void
247 pdf_document_init (PdfDocument *document)
248 {
249 }
250