]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/dvi-document.c
Use an EvRenderContext for rendering thumbnails instead of a suggested
[evince.git] / backend / 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 "dvi-document.h"
21 #include "ev-document-thumbnails.h"
22 #include "ev-document-misc.h"
23
24 #include "mdvi.h"
25 #include "fonts.h"
26 #include "pixbuf-device.h"
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.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         gchar *uri;
57 };
58
59 typedef struct _DviDocumentClass DviDocumentClass;
60
61 static void dvi_document_document_iface_init (EvDocumentIface *iface);
62 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
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       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
73      });
74
75 static gboolean
76 dvi_document_load (EvDocument  *document,
77                       const char  *uri,
78                       GError     **error)
79 {
80     gchar *filename;
81     DviDocument *dvi_document = DVI_DOCUMENT(document);
82     
83     filename = g_filename_from_uri (uri, NULL, error);
84     
85     if (!filename) {
86                 g_set_error (error,
87                              EV_DOCUMENT_ERROR,
88                              EV_DOCUMENT_ERROR_INVALID,
89                              _("File not available"));
90                 return FALSE;
91     }
92         
93     g_mutex_lock (dvi_context_mutex);
94     if (dvi_document->context)
95         mdvi_destroy_context (dvi_document->context);
96
97     dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
98     g_mutex_unlock (dvi_context_mutex);
99
100     if (!dvi_document->context) {
101                 g_set_error (error,
102                              EV_DOCUMENT_ERROR,
103                              EV_DOCUMENT_ERROR_INVALID,
104                              _("DVI document has incorrect format"));
105                 return FALSE;
106     }
107
108     mdvi_pixbuf_device_init (&dvi_document->context->device);
109
110     dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv 
111                 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
112                 
113     dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv 
114                 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
115
116     g_free (dvi_document->uri);
117     dvi_document->uri = g_strdup (uri);
118
119     return TRUE;
120 }
121
122
123 static gboolean
124 dvi_document_save (EvDocument  *document,
125                       const char  *uri,
126                       GError     **error)
127 {
128         DviDocument *dvi_document = DVI_DOCUMENT (document);
129
130         return ev_xfer_uri_simple (dvi_document->uri, uri, error);
131 }
132
133 static int
134 dvi_document_get_n_pages (EvDocument  *document)
135 {
136     DviDocument *dvi_document = DVI_DOCUMENT (document);
137     return dvi_document->context->npages;
138 }
139
140 static void
141 dvi_document_get_page_size (EvDocument   *document,
142                             int       page,
143                             double    *width,
144                             double    *height)
145 {
146         DviDocument * dvi_document = DVI_DOCUMENT (document);   
147
148         *width = dvi_document->base_width;
149         *height = dvi_document->base_height;;
150                                     
151         return;
152 }
153
154 static GdkPixbuf *
155 dvi_document_render_pixbuf (EvDocument  *document,
156                             EvRenderContext *rc)
157 {
158         GdkPixbuf *pixbuf;
159         GdkPixbuf *rotated_pixbuf;
160
161         DviDocument *dvi_document = DVI_DOCUMENT(document);
162
163         gint required_width, required_height;
164         gint proposed_width, proposed_height;
165         gint xmargin = 0, ymargin = 0;
166
167         /* We should protect our context since it's not 
168          * thread safe. The work to the future - 
169          * let context render page independently
170          */
171         g_mutex_lock (dvi_context_mutex);
172         
173         mdvi_setpage(dvi_document->context,  rc->page);
174         
175         mdvi_set_shrink (dvi_document->context, 
176                          (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
177                          (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
178
179         required_width = dvi_document->base_width * rc->scale;
180         required_height = dvi_document->base_height * rc->scale;
181         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
182         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
183         
184         if (required_width >= proposed_width)
185             xmargin = (required_width - proposed_width) / 2;
186         if (required_height >= proposed_height)
187             ymargin = (required_height - proposed_height) / 2;
188             
189         mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
190
191         mdvi_pixbuf_device_render (dvi_document->context);
192         
193         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
194
195         g_mutex_unlock (dvi_context_mutex);
196
197         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
198         g_object_unref (pixbuf);
199
200         return rotated_pixbuf;
201 }
202
203 static void
204 dvi_document_finalize (GObject *object)
205 {       
206         DviDocument *dvi_document = DVI_DOCUMENT(object);
207         
208         g_mutex_lock (dvi_context_mutex);
209         if (dvi_document->context)
210             {
211                 mdvi_pixbuf_device_free (&dvi_document->context->device);
212                 mdvi_destroy_context (dvi_document->context);
213             }
214         g_mutex_unlock (dvi_context_mutex);
215
216         if (dvi_document->params)
217                 g_free (dvi_document->params);
218
219         g_free (dvi_document->uri);
220                 
221         G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
222 }
223
224
225 static void
226 dvi_document_class_init (DviDocumentClass *klass)
227 {
228         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
229
230         gobject_class->finalize = dvi_document_finalize;
231
232         mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
233         mdvi_register_fonts ();
234
235         dvi_context_mutex = g_mutex_new ();
236 }
237
238 static gboolean
239 dvi_document_can_get_text (EvDocument *document)
240 {
241         return FALSE;
242 }
243
244 static EvDocumentInfo *
245 dvi_document_get_info (EvDocument *document)
246 {
247         EvDocumentInfo *info;
248
249         info = g_new0 (EvDocumentInfo, 1);
250
251         return info;
252 }
253
254 static void
255 dvi_document_document_iface_init (EvDocumentIface *iface)
256 {
257         iface->load = dvi_document_load;
258         iface->save = dvi_document_save;
259         iface->can_get_text = dvi_document_can_get_text;
260         iface->get_n_pages = dvi_document_get_n_pages;
261         iface->get_page_size = dvi_document_get_page_size;
262         iface->render_pixbuf = dvi_document_render_pixbuf;
263         iface->get_info = dvi_document_get_info;
264 }
265
266 static void
267 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
268                                         EvRenderContext      *rc, 
269                                         gint                  *width,
270                                         gint                  *height)
271 {       
272         DviDocument *dvi_document = DVI_DOCUMENT (document);
273         gdouble page_width = dvi_document->base_width;
274         gdouble page_height = dvi_document->base_height;
275
276         if (rc->rotation == 90 || rc->rotation == 270) {
277                 *width = (gint) (page_height * rc->scale);
278                 *height = (gint) (page_width * rc->scale);
279         } else {
280                 *width = (gint) (page_width * rc->scale);
281                 *height = (gint) (page_height * rc->scale);
282         }
283 }
284
285 static GdkPixbuf *
286 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
287                                        EvRenderContext      *rc,   
288                                        gboolean              border)
289 {
290         DviDocument *dvi_document = DVI_DOCUMENT (document);
291         GdkPixbuf *pixbuf;
292         GdkPixbuf *border_pixbuf;
293         GdkPixbuf *rotated_pixbuf;
294         gint thumb_width, thumb_height;
295         gint proposed_width, proposed_height;
296         
297         thumb_width = (gint) (dvi_document->base_width * rc->scale);
298         thumb_height = (gint) (dvi_document->base_height * rc->scale);
299         
300         g_mutex_lock (dvi_context_mutex);
301
302         mdvi_setpage (dvi_document->context, rc->page);
303
304         mdvi_set_shrink (dvi_document->context, 
305                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
306                           (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
307
308         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
309         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
310                           
311         if (border) {
312                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
313                                                  MAX (thumb_width - proposed_width, 0) / 2,
314                                                  MAX (thumb_height - proposed_height, 0) / 2);  
315         } else {
316                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
317                                                  MAX (thumb_width - proposed_width - 2, 0) / 2,
318                                                  MAX (thumb_height - proposed_height - 2, 0) / 2);      
319         }
320         
321
322         mdvi_pixbuf_device_render (dvi_document->context);
323         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
324
325         g_mutex_unlock (dvi_context_mutex);
326         
327         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
328         g_object_unref (pixbuf);
329         
330         if (border) {
331               GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
332               
333               rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
334               g_object_unref (tmp_pixbuf);
335         }
336
337         return rotated_pixbuf;
338 }
339
340 static void
341 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
342 {
343         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
344         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
345 }
346
347
348 static void
349 dvi_document_init_params (DviDocument *dvi_document)
350 {       
351         dvi_document->params = g_new0 (DviParams, 1);   
352
353         dvi_document->params->dpi      = MDVI_DPI;
354         dvi_document->params->vdpi     = MDVI_VDPI;
355         dvi_document->params->mag      = MDVI_MAGNIFICATION;
356         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
357         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
358         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
359         dvi_document->params->hdrift   = 0;
360         dvi_document->params->vdrift   = 0;
361         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
362         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
363         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
364
365         dvi_document->spec = NULL;
366         
367         dvi_document->params->bg = 0xffffffff;
368         dvi_document->params->fg = 0xff000000;
369 }
370
371 static void
372 dvi_document_init (DviDocument *dvi_document)
373 {
374         dvi_document->context = NULL;
375         dvi_document_init_params (dvi_document);
376 }