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;
43 GdkRectangle page_rect;
46 GDKSplashOutputDev *out;
51 static void pdf_document_document_iface_init (EvDocumentIface *iface);
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) });
59 pdf_document_load (EvDocument *document,
63 PdfDocument *pdf_document = PDF_DOCUMENT (document);
70 globalParams = new GlobalParams("/etc/xpdfrc");
71 globalParams->setupBaseFonts(NULL);
74 filename = g_filename_from_uri (uri, NULL, error);
78 filename_g = new GString (filename);
82 newDoc = new PDFDoc(filename_g, 0, 0);
86 if (!newDoc->isOk()) {
87 err = newDoc->getErrorCode();
90 /* FIXME: Add a real error enum to EvDocument */
91 g_set_error (error, G_FILE_ERROR,
93 "Failed to load document (error %d) '%s'\n",
100 if (pdf_document->doc)
101 delete pdf_document->doc;
102 pdf_document->doc = newDoc;
104 if (pdf_document->out) {
105 pdf_document->out->startDoc(pdf_document->doc->getXRef());
106 pdf_document->doc->displayPage (pdf_document->out, 1, 72, 72, 0, gTrue, gTrue);
113 pdf_document_get_n_pages (EvDocument *document)
115 PdfDocument *pdf_document = PDF_DOCUMENT (document);
117 if (pdf_document->doc)
118 return pdf_document->doc->getNumPages();
124 pdf_document_set_page (EvDocument *document,
127 PdfDocument *pdf_document = PDF_DOCUMENT (document);
129 pdf_document->page = page;
133 redraw_callback (void *data)
135 /* Need to hook up through a EvDocument callback? */
139 pdf_document_set_target (EvDocument *document,
142 PdfDocument *pdf_document = PDF_DOCUMENT (document);
144 if (pdf_document->target != target) {
145 if (pdf_document->target)
146 g_object_unref (pdf_document->target);
148 pdf_document->target = target;
150 if (pdf_document->target)
151 g_object_ref (pdf_document->target);
153 if (pdf_document->out)
154 delete pdf_document->out;
156 if (pdf_document->target) {
157 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
158 redraw_callback, (void*) document);
160 if (pdf_document->doc) {
161 pdf_document->out->startDoc(pdf_document->doc->getXRef());
162 pdf_document->doc->displayPage (pdf_document->out, 1, 72, 72, 0, gTrue, gTrue);
169 pdf_document_set_page_rect (EvDocument *document,
175 PdfDocument *pdf_document = PDF_DOCUMENT (document);
177 pdf_document->page_rect.x = x;
178 pdf_document->page_rect.y = y;
179 pdf_document->page_rect.width = width;
180 pdf_document->page_rect.height = height;
184 pdf_document_render (EvDocument *document,
190 PdfDocument *pdf_document = PDF_DOCUMENT (document);
194 if (!pdf_document->target)
199 page.width = pdf_document->out->getBitmapWidth();
200 page.height = pdf_document->out->getBitmapHeight();
204 draw.width = clip_width;
205 draw.height = clip_height;
207 if (gdk_rectangle_intersect (&page, &draw, &draw))
208 pdf_document->out->redraw (draw.x, draw.y,
209 pdf_document->target,
211 draw.width, draw.height);
215 pdf_document_finalize (GObject *object)
217 PdfDocument *pdf_document = PDF_DOCUMENT (object);
219 if (pdf_document->target)
220 g_object_unref (pdf_document->target);
222 if (pdf_document->out)
223 delete pdf_document->out;
224 if (pdf_document->doc)
225 delete pdf_document->doc;
230 pdf_document_class_init (PdfDocumentClass *klass)
232 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
234 gobject_class->finalize = pdf_document_finalize;
238 pdf_document_document_iface_init (EvDocumentIface *iface)
240 iface->load = pdf_document_load;
241 iface->get_n_pages = pdf_document_get_n_pages;
242 iface->set_page = pdf_document_set_page;
243 iface->set_target = pdf_document_set_target;
244 iface->set_page_rect = pdf_document_set_page_rect;
245 iface->render = pdf_document_render;
249 pdf_document_init (PdfDocument *document)