1 /* pdfdocument.h: Implementation of EvDocument for PDF
2 * Copyright (C) 2004, Red Hat, Inc.
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)
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.
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.
19 #include "gpdf-g-switch.h"
20 #include "pdf-document.h"
21 #include "gpdf-g-switch.h"
23 #include "GlobalParams.h"
24 #include "GDKSplashOutputDev.h"
27 typedef struct _PdfDocumentClass PdfDocumentClass;
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))
33 struct _PdfDocumentClass
35 GObjectClass parent_class;
40 GObject parent_instance;
48 GDKSplashOutputDev *out;
54 static void pdf_document_document_iface_init (EvDocumentIface *iface);
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) });
61 document_validate_page (PdfDocument *pdf_document)
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,
69 pdf_document->page_valid = TRUE;
74 pdf_document_load (EvDocument *document,
78 PdfDocument *pdf_document = PDF_DOCUMENT (document);
85 globalParams = new GlobalParams("/etc/xpdfrc");
86 globalParams->setupBaseFonts(NULL);
89 filename = g_filename_from_uri (uri, NULL, error);
93 filename_g = new GString (filename);
96 // open the PDF file, assumes ownership of filename_g
97 newDoc = new PDFDoc(filename_g, 0, 0);
99 if (!newDoc->isOk()) {
100 err = newDoc->getErrorCode();
103 /* FIXME: Add a real error enum to EvDocument */
104 g_set_error (error, G_FILE_ERROR,
106 "Failed to load document (error %d) '%s'\n",
113 if (pdf_document->doc)
114 delete pdf_document->doc;
115 pdf_document->doc = newDoc;
117 pdf_document->page = 1;
119 if (pdf_document->out)
120 pdf_document->out->startDoc(pdf_document->doc->getXRef());
122 pdf_document->page_valid = FALSE;
128 pdf_document_get_n_pages (EvDocument *document)
130 PdfDocument *pdf_document = PDF_DOCUMENT (document);
132 if (pdf_document->doc)
133 return pdf_document->doc->getNumPages();
139 pdf_document_set_page (EvDocument *document,
142 PdfDocument *pdf_document = PDF_DOCUMENT (document);
144 if (page != pdf_document->page) {
145 pdf_document->page = page;
146 pdf_document->page_valid = FALSE;
152 redraw_callback (void *data)
154 /* Need to hook up through a EvDocument callback? */
158 pdf_document_set_target (EvDocument *document,
161 PdfDocument *pdf_document = PDF_DOCUMENT (document);
163 if (pdf_document->target != target) {
164 if (pdf_document->target)
165 g_object_unref (pdf_document->target);
167 pdf_document->target = target;
169 if (pdf_document->target)
170 g_object_ref (pdf_document->target);
172 if (pdf_document->out) {
173 delete pdf_document->out;
174 pdf_document->out = NULL;
177 if (pdf_document->target) {
178 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
179 redraw_callback, (void*) document);
181 if (pdf_document->doc)
182 pdf_document->out->startDoc(pdf_document->doc->getXRef());
186 pdf_document->page_valid = FALSE;
191 pdf_document_set_scale (EvDocument *document,
194 PdfDocument *pdf_document = PDF_DOCUMENT (document);
196 if (pdf_document->scale != scale) {
197 pdf_document->scale = scale;
198 pdf_document->page_valid = FALSE;
203 pdf_document_set_page_offset (EvDocument *document,
207 PdfDocument *pdf_document = PDF_DOCUMENT (document);
209 pdf_document->page_x_offset = x;
210 pdf_document->page_y_offset = y;
214 pdf_document_get_page_size (EvDocument *document,
218 PdfDocument *pdf_document = PDF_DOCUMENT (document);
220 if (document_validate_page (pdf_document)) {
222 *width = pdf_document->out->getBitmapWidth();
224 *height = pdf_document->out->getBitmapHeight();
234 pdf_document_render (EvDocument *document,
240 PdfDocument *pdf_document = PDF_DOCUMENT (document);
244 if (!document_validate_page (pdf_document) || !pdf_document->target)
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();
254 draw.width = clip_width;
255 draw.height = clip_height;
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,
261 draw.width, draw.height);
265 pdf_document_finalize (GObject *object)
267 PdfDocument *pdf_document = PDF_DOCUMENT (object);
269 if (pdf_document->target)
270 g_object_unref (pdf_document->target);
272 if (pdf_document->out)
273 delete pdf_document->out;
274 if (pdf_document->doc)
275 delete pdf_document->doc;
280 pdf_document_class_init (PdfDocumentClass *klass)
282 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
284 gobject_class->finalize = pdf_document_finalize;
288 pdf_document_document_iface_init (EvDocumentIface *iface)
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;
301 pdf_document_init (PdfDocument *pdf_document)
303 pdf_document->page = 1;
304 pdf_document->page_x_offset = 0;
305 pdf_document->page_y_offset = 0;
306 pdf_document->scale = 1.;
308 pdf_document->page_valid = FALSE;