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);
81 // open the PDF file, assumes ownership of filename_g
82 newDoc = new PDFDoc(filename_g, 0, 0);
84 if (!newDoc->isOk()) {
85 err = newDoc->getErrorCode();
88 /* FIXME: Add a real error enum to EvDocument */
89 g_set_error (error, G_FILE_ERROR,
91 "Failed to load document (error %d) '%s'\n",
98 if (pdf_document->doc)
99 delete pdf_document->doc;
100 pdf_document->doc = newDoc;
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);
111 pdf_document_get_n_pages (EvDocument *document)
113 PdfDocument *pdf_document = PDF_DOCUMENT (document);
115 if (pdf_document->doc)
116 return pdf_document->doc->getNumPages();
122 pdf_document_set_page (EvDocument *document,
125 PdfDocument *pdf_document = PDF_DOCUMENT (document);
127 pdf_document->page = page;
131 redraw_callback (void *data)
133 /* Need to hook up through a EvDocument callback? */
137 pdf_document_set_target (EvDocument *document,
140 PdfDocument *pdf_document = PDF_DOCUMENT (document);
142 if (pdf_document->target != target) {
143 if (pdf_document->target)
144 g_object_unref (pdf_document->target);
146 pdf_document->target = target;
148 if (pdf_document->target)
149 g_object_ref (pdf_document->target);
151 if (pdf_document->out)
152 delete pdf_document->out;
154 if (pdf_document->target) {
155 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
156 redraw_callback, (void*) document);
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);
167 pdf_document_set_page_rect (EvDocument *document,
173 PdfDocument *pdf_document = PDF_DOCUMENT (document);
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;
182 pdf_document_render (EvDocument *document,
188 PdfDocument *pdf_document = PDF_DOCUMENT (document);
192 if (!pdf_document->target)
197 page.width = pdf_document->out->getBitmapWidth();
198 page.height = pdf_document->out->getBitmapHeight();
202 draw.width = clip_width;
203 draw.height = clip_height;
205 if (gdk_rectangle_intersect (&page, &draw, &draw))
206 pdf_document->out->redraw (draw.x, draw.y,
207 pdf_document->target,
209 draw.width, draw.height);
213 pdf_document_finalize (GObject *object)
215 PdfDocument *pdf_document = PDF_DOCUMENT (object);
217 if (pdf_document->target)
218 g_object_unref (pdf_document->target);
220 if (pdf_document->out)
221 delete pdf_document->out;
222 if (pdf_document->doc)
223 delete pdf_document->doc;
228 pdf_document_class_init (PdfDocumentClass *klass)
230 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
232 gobject_class->finalize = pdf_document_finalize;
236 pdf_document_document_iface_init (EvDocumentIface *iface)
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;
247 pdf_document_init (PdfDocument *document)