]> www.fi.muni.cz Git - evince.git/blob - dvi/dvi-document.c
Recent files support.
[evince.git] / dvi / dvi-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21
22 #include "dvi-document.h"
23 #include "ev-document-thumbnails.h"
24
25 #include "mdvi.h"
26 #include "fonts.h"
27 #include "pixbuf-device.h"
28
29 #include <gtk/gtk.h>
30
31 GMutex *dvi_context_mutex = NULL;
32
33 enum {
34         PROP_0,
35         PROP_TITLE
36 };
37
38 struct _DviDocumentClass
39 {
40         GObjectClass parent_class;
41 };
42
43 struct _DviDocument
44 {
45         GObject parent_instance;
46
47         DviContext *context;
48         DviPageSpec *spec;
49         DviParams *params;
50         
51         /* To let document scale we should remember width and height */
52         
53         double base_width;
54         double base_height;
55 };
56
57 typedef struct _DviDocumentClass DviDocumentClass;
58
59 static void dvi_document_document_iface_init (EvDocumentIface *iface);
60 #if 0
61 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
62 #endif
63 static void dvi_document_get_page_size                  (EvDocument   *document,
64                                                          int       page,
65                                                          double    *width,
66                                                          double    *height);
67
68 G_DEFINE_TYPE_WITH_CODE 
69     (DviDocument, dvi_document, G_TYPE_OBJECT, 
70     {
71       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);    
72 #if 0
73       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
74 #endif      
75      });
76
77 static gboolean
78 dvi_document_load (EvDocument  *document,
79                       const char  *uri,
80                       GError     **error)
81 {
82     gchar *filename;
83     DviDocument *dvi_document = DVI_DOCUMENT(document);
84     
85     filename = g_filename_from_uri (uri, NULL, error);
86     
87     if (!filename)
88         return FALSE;
89         
90     if (dvi_document->context)
91         mdvi_destroy_context (dvi_document->context);
92
93     dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
94
95     mdvi_pixbuf_device_init (&dvi_document->context->device);
96
97     dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv 
98                 + 2 * unit2pix(dvi_document->params->dpi, MDVI_VMARGIN) / dvi_document->params->hshrink;
99                 
100     dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv 
101                 + 2 * unit2pix(dvi_document->params->dpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
102
103     dvi_context_mutex = g_mutex_new ();
104
105     return TRUE;
106 }
107
108
109 static gboolean
110 dvi_document_save (EvDocument  *document,
111                       const char  *uri,
112                       GError     **error)
113 {
114         g_warning ("dvi_document_save not implemented"); /* FIXME */
115         return TRUE;
116 }
117
118 static int
119 dvi_document_get_n_pages (EvDocument  *document)
120 {
121     DviDocument *dvi_document = DVI_DOCUMENT (document);
122     return dvi_document->context->npages;
123 }
124
125 static void
126 dvi_document_get_page_size (EvDocument   *document,
127                             int       page,
128                             double    *width,
129                             double    *height)
130 {
131         DviDocument * dvi_document = DVI_DOCUMENT (document);   
132         
133         if (width != NULL)
134             *width = dvi_document->base_width;
135
136         if (height != NULL)
137             *height = dvi_document->base_height;
138                             
139         return;
140 }
141
142 static GdkPixbuf *
143 dvi_document_render_pixbuf (EvDocument  *document, int page, double scale)
144 {
145         GdkPixbuf *pixbuf;
146
147         DviDocument *dvi_document = DVI_DOCUMENT(document);
148
149         gint required_width, required_height;
150         gint proposed_width, proposed_height;
151         gint xmargin = 0, ymargin = 0;
152
153         /* We should protect our context since it's not 
154          * thread safe. The work to the future - 
155          * let context render page independently
156          */
157         g_mutex_lock (dvi_context_mutex);
158         
159         mdvi_setpage(dvi_document->context,  page);
160         
161         mdvi_set_shrink (dvi_document->context, 
162                          (int)((dvi_document->params->hshrink - 1) / scale) + 1,
163                          (int)((dvi_document->params->vshrink - 1) / scale) + 1);
164
165         required_width = dvi_document->base_width * scale;
166         required_height = dvi_document->base_height * scale;
167         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
168         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
169         
170         if (required_width >= proposed_width)
171             xmargin = (required_width - proposed_width) / 2;
172         if (required_height >= proposed_height)
173             ymargin = (required_height - proposed_height) / 2;
174             
175         mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
176
177         mdvi_pixbuf_device_render (dvi_document->context);
178         
179         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
180
181         g_mutex_unlock (dvi_context_mutex);
182
183         return pixbuf;
184 }
185
186 static void
187 dvi_document_finalize (GObject *object)
188 {       
189         DviDocument *dvi_document = DVI_DOCUMENT(object);
190
191         if (dvi_document->context)
192             {
193                 mdvi_pixbuf_device_free (&dvi_document->context->device);
194                 mdvi_destroy_context (dvi_document->context);
195             }
196
197         if (dvi_document->params)
198                 g_free (dvi_document->params);
199                 
200         G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
201 }
202
203 static void
204 dvi_document_set_property (GObject *object,
205                               guint prop_id,
206                               const GValue *value,
207                               GParamSpec *pspec)
208 {
209         switch (prop_id)
210         {
211                 case PROP_TITLE:
212                         /* read only */
213                         break;
214         }
215 }
216
217 static void
218 dvi_document_get_property (GObject *object,
219                               guint prop_id,
220                               GValue *value,
221                               GParamSpec *pspec)
222 {
223         switch (prop_id)
224         {
225                 case PROP_TITLE:
226                         g_value_set_string (value, NULL);
227                         break;
228         }
229 }
230
231 static void
232 dvi_document_class_init (DviDocumentClass *klass)
233 {
234         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
235
236         gobject_class->finalize = dvi_document_finalize;
237         gobject_class->get_property = dvi_document_get_property;
238         gobject_class->set_property = dvi_document_set_property;
239
240         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
241 }
242
243 static char *
244 dvi_document_get_text (EvDocument *document, gint page, EvRectangle *rect)
245 {
246         /* FIXME this method should not be in EvDocument */
247         g_warning ("dvi_document_get_text not implemented");
248         return NULL;
249 }
250
251 static void
252 dvi_document_document_iface_init (EvDocumentIface *iface)
253 {
254         iface->load = dvi_document_load;
255         iface->save = dvi_document_save;
256         iface->get_text = dvi_document_get_text;
257         iface->get_n_pages = dvi_document_get_n_pages;
258         iface->get_page_size = dvi_document_get_page_size;
259         iface->render_pixbuf = dvi_document_render_pixbuf;
260 }
261
262 #if 0
263 static void
264 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
265                                            gint                  page,
266                                            gint                  suggested_width,
267                                            gint                  *width,
268                                            gint                  *height)
269 {
270         return;
271 }
272
273 static GdkPixbuf *
274 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
275                                           gint                   page,
276                                           gint                   width)
277 {
278
279         return NULL;
280 }
281
282 static void
283 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
284 {
285         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
286         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
287 }
288 #endif
289
290
291 static void
292 dvi_document_init_params (DviDocument *dvi_document)
293 {       
294         dvi_document->params = g_new0 (DviParams, 1);   
295
296         dvi_document->params->dpi      = MDVI_DPI;
297         dvi_document->params->vdpi     = MDVI_VDPI;
298         dvi_document->params->mag      = MDVI_MAGNIFICATION;
299         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
300         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
301         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
302         dvi_document->params->hdrift   = 0;
303         dvi_document->params->vdrift   = 0;
304         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
305         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
306         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
307
308         dvi_document->spec = NULL;
309         
310         dvi_document->params->bg = 0xffffffff;
311         dvi_document->params->fg = 0xff000000;
312
313         mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
314         
315         mdvi_register_fonts ();
316 }
317
318 static void
319 dvi_document_init (DviDocument *dvi_document)
320 {
321         dvi_document->context = NULL;
322         dvi_document_init_params (dvi_document);
323 }