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