]> www.fi.muni.cz Git - evince.git/blob - dvi/dvi-document.c
f827ff40ace36608626108236e8b3bce9b7e2606
[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 #include "ev-document-misc.h"
25
26 #include "mdvi.h"
27 #include "fonts.h"
28 #include "pixbuf-device.h"
29
30 #include <gtk/gtk.h>
31
32 GMutex *dvi_context_mutex = NULL;
33
34 enum {
35         PROP_0,
36         PROP_TITLE
37 };
38
39 struct _DviDocumentClass
40 {
41         GObjectClass parent_class;
42 };
43
44 struct _DviDocument
45 {
46         GObject parent_instance;
47
48         DviContext *context;
49         DviPageSpec *spec;
50         DviParams *params;
51         
52         /* To let document scale we should remember width and height */
53         
54         double base_width;
55         double base_height;
56 };
57
58 typedef struct _DviDocumentClass DviDocumentClass;
59
60 static void dvi_document_document_iface_init (EvDocumentIface *iface);
61 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
62 static void dvi_document_get_page_size                  (EvDocument   *document,
63                                                          int       page,
64                                                          double    *width,
65                                                          double    *height);
66
67 G_DEFINE_TYPE_WITH_CODE 
68     (DviDocument, dvi_document, G_TYPE_OBJECT, 
69     {
70       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);    
71       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
72      });
73
74 static gboolean
75 dvi_document_load (EvDocument  *document,
76                       const char  *uri,
77                       GError     **error)
78 {
79     gchar *filename;
80     DviDocument *dvi_document = DVI_DOCUMENT(document);
81     
82     filename = g_filename_from_uri (uri, NULL, error);
83     
84     if (!filename)
85         return FALSE;
86         
87     if (dvi_document->context)
88         mdvi_destroy_context (dvi_document->context);
89
90     dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
91
92     mdvi_pixbuf_device_init (&dvi_document->context->device);
93
94     dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv 
95                 + 2 * unit2pix(dvi_document->params->dpi, MDVI_VMARGIN) / dvi_document->params->hshrink;
96                 
97     dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv 
98                 + 2 * unit2pix(dvi_document->params->dpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
99
100     dvi_context_mutex = g_mutex_new ();
101
102
103     return TRUE;
104 }
105
106
107 static gboolean
108 dvi_document_save (EvDocument  *document,
109                       const char  *uri,
110                       GError     **error)
111 {
112         g_warning ("dvi_document_save not implemented"); /* FIXME */
113         return TRUE;
114 }
115
116 static int
117 dvi_document_get_n_pages (EvDocument  *document)
118 {
119     DviDocument *dvi_document = DVI_DOCUMENT (document);
120     return dvi_document->context->npages;
121 }
122
123 static void
124 dvi_document_get_page_size (EvDocument   *document,
125                             int       page,
126                             double    *width,
127                             double    *height)
128 {
129         DviDocument * dvi_document = DVI_DOCUMENT (document);   
130         
131         if (width != NULL)
132             *width = dvi_document->base_width;
133
134         if (height != NULL)
135             *height = dvi_document->base_height;
136                             
137         return;
138 }
139
140 static GdkPixbuf *
141 dvi_document_render_pixbuf (EvDocument  *document, int page, double scale)
142 {
143         GdkPixbuf *pixbuf;
144
145         DviDocument *dvi_document = DVI_DOCUMENT(document);
146
147         gint required_width, required_height;
148         gint proposed_width, proposed_height;
149         gint xmargin = 0, ymargin = 0;
150
151         /* We should protect our context since it's not 
152          * thread safe. The work to the future - 
153          * let context render page independently
154          */
155         g_mutex_lock (dvi_context_mutex);
156         
157         mdvi_setpage(dvi_document->context,  page);
158         
159         mdvi_set_shrink (dvi_document->context, 
160                          (int)((dvi_document->params->hshrink - 1) / scale) + 1,
161                          (int)((dvi_document->params->vshrink - 1) / scale) + 1);
162
163         required_width = dvi_document->base_width * scale;
164         required_height = dvi_document->base_height * scale;
165         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
166         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
167         
168         if (required_width >= proposed_width)
169             xmargin = (required_width - proposed_width) / 2;
170         if (required_height >= proposed_height)
171             ymargin = (required_height - proposed_height) / 2;
172             
173         mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
174
175         mdvi_pixbuf_device_render (dvi_document->context);
176         
177         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
178
179         g_mutex_unlock (dvi_context_mutex);
180
181         return pixbuf;
182 }
183
184 static void
185 dvi_document_finalize (GObject *object)
186 {       
187         DviDocument *dvi_document = DVI_DOCUMENT(object);
188
189         if (dvi_document->context)
190             {
191                 mdvi_pixbuf_device_free (&dvi_document->context->device);
192                 mdvi_destroy_context (dvi_document->context);
193             }
194
195         if (dvi_document->params)
196                 g_free (dvi_document->params);
197                 
198         G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
199 }
200
201 static void
202 dvi_document_set_property (GObject *object,
203                               guint prop_id,
204                               const GValue *value,
205                               GParamSpec *pspec)
206 {
207         switch (prop_id)
208         {
209                 case PROP_TITLE:
210                         /* read only */
211                         break;
212         }
213 }
214
215 static void
216 dvi_document_get_property (GObject *object,
217                               guint prop_id,
218                               GValue *value,
219                               GParamSpec *pspec)
220 {
221         switch (prop_id)
222         {
223                 case PROP_TITLE:
224                         g_value_set_string (value, NULL);
225                         break;
226         }
227 }
228
229 static void
230 dvi_document_class_init (DviDocumentClass *klass)
231 {
232         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
233
234         gobject_class->finalize = dvi_document_finalize;
235         gobject_class->get_property = dvi_document_get_property;
236         gobject_class->set_property = dvi_document_set_property;
237
238         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
239 }
240
241 static char *
242 dvi_document_get_text (EvDocument *document, gint page, EvRectangle *rect)
243 {
244         /* FIXME this method should not be in EvDocument */
245         g_warning ("dvi_document_get_text not implemented");
246         return NULL;
247 }
248
249 static void
250 dvi_document_document_iface_init (EvDocumentIface *iface)
251 {
252         iface->load = dvi_document_load;
253         iface->save = dvi_document_save;
254         iface->get_text = dvi_document_get_text;
255         iface->get_n_pages = dvi_document_get_n_pages;
256         iface->get_page_size = dvi_document_get_page_size;
257         iface->render_pixbuf = dvi_document_render_pixbuf;
258 }
259
260 static void
261 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
262                                            gint                  page,
263                                            gint                  suggested_width,
264                                            gint                  *width,
265                                            gint                  *height)
266 {       
267         DviDocument *dvi_document = DVI_DOCUMENT (document); 
268         gdouble page_ratio;
269         
270         page_ratio = dvi_document->base_height / dvi_document->base_width;
271         *width = suggested_width;
272         *height = (gint) (suggested_width * page_ratio);
273
274         return;
275 }
276
277 static GdkPixbuf *
278 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
279                                        gint                      page,
280                                        gint                      width,
281                                        gboolean                  border)
282 {
283         DviDocument *dvi_document = DVI_DOCUMENT (document);
284         GdkPixbuf *pixbuf;
285         GdkPixbuf *border_pixbuf;
286         gint thumb_width, thumb_height;
287         gint proposed_width, proposed_height;
288         
289         dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
290
291         g_mutex_lock (dvi_context_mutex);
292
293         mdvi_setpage(dvi_document->context,  page);
294
295         mdvi_set_shrink (dvi_document->context, 
296                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
297                           (int)dvi_document->base_width * dvi_document->params->vshrink / thumb_height);
298
299         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
300         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
301                           
302         if (border) {
303                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
304                                                  MAX (thumb_width - proposed_width, 0) / 2,
305                                                  MAX (thumb_height - proposed_height, 0) / 2);  
306         } else {
307                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
308                                                  MAX (thumb_width - proposed_width - 2, 0) / 2,
309                                                  MAX (thumb_height - proposed_height - 2, 0) / 2);      
310         }
311         
312
313         mdvi_pixbuf_device_render (dvi_document->context);
314         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
315
316         g_mutex_unlock (dvi_context_mutex);
317
318         if (border) {
319                 border_pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
320                 gdk_pixbuf_copy_area (pixbuf, 0, 0, 
321                                       thumb_width - 2, thumb_height - 2,
322                                       border_pixbuf, 2, 2);
323                 g_object_unref (pixbuf);
324                 pixbuf = border_pixbuf;
325         }
326         
327         
328         return pixbuf;
329 }
330
331 static void
332 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
333 {
334         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
335         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
336 }
337
338
339 static void
340 dvi_document_init_params (DviDocument *dvi_document)
341 {       
342         dvi_document->params = g_new0 (DviParams, 1);   
343
344         dvi_document->params->dpi      = MDVI_DPI;
345         dvi_document->params->vdpi     = MDVI_VDPI;
346         dvi_document->params->mag      = MDVI_MAGNIFICATION;
347         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
348         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
349         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
350         dvi_document->params->hdrift   = 0;
351         dvi_document->params->vdrift   = 0;
352         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
353         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
354         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
355
356         dvi_document->spec = NULL;
357         
358         dvi_document->params->bg = 0xffffffff;
359         dvi_document->params->fg = 0xff000000;
360
361         mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
362         
363         mdvi_register_fonts ();
364 }
365
366 static void
367 dvi_document_init (DviDocument *dvi_document)
368 {
369         dvi_document->context = NULL;
370         dvi_document_init_params (dvi_document);
371 }