]> 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         EvOrientation orientation;
59 };
60
61 typedef struct _DviDocumentClass DviDocumentClass;
62
63 static void dvi_document_document_iface_init (EvDocumentIface *iface);
64 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
65 static void dvi_document_get_page_size                  (EvDocument   *document,
66                                                          int       page,
67                                                          double    *width,
68                                                          double    *height);
69
70 G_DEFINE_TYPE_WITH_CODE 
71     (DviDocument, dvi_document, G_TYPE_OBJECT, 
72     {
73       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);    
74       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
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                 g_set_error (error,
89                              EV_DOCUMENT_ERROR,
90                              EV_DOCUMENT_ERROR_INVALID,
91                              _("File not available"));
92                 return FALSE;
93     }
94         
95     if (dvi_document->context)
96         mdvi_destroy_context (dvi_document->context);
97
98     dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
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     dvi_context_mutex = g_mutex_new ();
117
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         g_warning ("dvi_document_save not implemented"); /* FIXME */
129         return TRUE;
130 }
131
132 static int
133 dvi_document_get_n_pages (EvDocument  *document)
134 {
135     DviDocument *dvi_document = DVI_DOCUMENT (document);
136     return dvi_document->context->npages;
137 }
138
139 static EvOrientation
140 dvi_document_get_orientation (EvDocument *document)
141 {
142         DviDocument *dvi_document = DVI_DOCUMENT (document);
143
144         return dvi_document->orientation;
145 }
146
147 static void
148 dvi_document_set_orientation (EvDocument *document,
149                               EvOrientation   orientation)
150 {
151         DviDocument *dvi_document = DVI_DOCUMENT (document);
152
153         dvi_document->orientation = orientation;
154 }
155
156
157 static void
158 dvi_document_get_page_size (EvDocument   *document,
159                             int       page,
160                             double    *width,
161                             double    *height)
162 {
163         DviDocument * dvi_document = DVI_DOCUMENT (document);   
164
165         if (dvi_document->orientation == EV_ORIENTATION_PORTRAIT ||
166             dvi_document->orientation ==  EV_ORIENTATION_UPSIDEDOWN) {
167                 *width = dvi_document->base_width;
168                 *height = dvi_document->base_height;;
169         } else {
170                 *width = dvi_document->base_height;
171                 *height = dvi_document->base_width;
172         }
173                                     
174         return;
175 }
176
177 static GdkPixbuf *
178 rotate_pixbuf (EvDocument *document, GdkPixbuf *pixbuf)
179 {
180         DviDocument *dvi_document = DVI_DOCUMENT (document);
181
182         switch (dvi_document->orientation)
183         {
184                 case EV_ORIENTATION_LANDSCAPE:
185                         return gdk_pixbuf_rotate_simple (pixbuf, 90);
186                 case EV_ORIENTATION_UPSIDEDOWN:
187                         return gdk_pixbuf_rotate_simple (pixbuf, 180);
188                 case EV_ORIENTATION_SEASCAPE:
189                         return gdk_pixbuf_rotate_simple (pixbuf, 270);
190                 default:
191                         return g_object_ref (pixbuf);
192         }
193 }
194
195 static GdkPixbuf *
196 dvi_document_render_pixbuf (EvDocument  *document,
197                             EvRenderContext *rc)
198 {
199         GdkPixbuf *pixbuf;
200         GdkPixbuf *rotated_pixbuf;
201
202         DviDocument *dvi_document = DVI_DOCUMENT(document);
203
204         gint required_width, required_height;
205         gint proposed_width, proposed_height;
206         gint xmargin = 0, ymargin = 0;
207
208         /* We should protect our context since it's not 
209          * thread safe. The work to the future - 
210          * let context render page independently
211          */
212         g_mutex_lock (dvi_context_mutex);
213         
214         mdvi_setpage(dvi_document->context,  rc->page);
215         
216         mdvi_set_shrink (dvi_document->context, 
217                          (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
218                          (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
219
220         required_width = dvi_document->base_width * rc->scale;
221         required_height = dvi_document->base_height * rc->scale;
222         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
223         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
224         
225         if (required_width >= proposed_width)
226             xmargin = (required_width - proposed_width) / 2;
227         if (required_height >= proposed_height)
228             ymargin = (required_height - proposed_height) / 2;
229             
230         mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
231
232         mdvi_pixbuf_device_render (dvi_document->context);
233         
234         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
235
236         g_mutex_unlock (dvi_context_mutex);
237
238         rotated_pixbuf = rotate_pixbuf (document, pixbuf);
239         g_object_unref (pixbuf);
240
241         return rotated_pixbuf;
242 }
243
244 static void
245 dvi_document_finalize (GObject *object)
246 {       
247         DviDocument *dvi_document = DVI_DOCUMENT(object);
248
249         if (dvi_document->context)
250             {
251                 mdvi_pixbuf_device_free (&dvi_document->context->device);
252                 mdvi_destroy_context (dvi_document->context);
253             }
254
255         if (dvi_document->params)
256                 g_free (dvi_document->params);
257                 
258         G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
259 }
260
261
262 static void
263 dvi_document_class_init (DviDocumentClass *klass)
264 {
265         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
266
267         gobject_class->finalize = dvi_document_finalize;
268 }
269
270 static gboolean
271 dvi_document_can_get_text (EvDocument *document)
272 {
273         return FALSE;
274 }
275
276 static EvDocumentInfo *
277 dvi_document_get_info (EvDocument *document)
278 {
279         EvDocumentInfo *info;
280
281         info = g_new0 (EvDocumentInfo, 1);
282
283         return info;
284 }
285
286 static void
287 dvi_document_document_iface_init (EvDocumentIface *iface)
288 {
289         iface->load = dvi_document_load;
290         iface->save = dvi_document_save;
291         iface->can_get_text = dvi_document_can_get_text;
292         iface->get_n_pages = dvi_document_get_n_pages;
293         iface->get_page_size = dvi_document_get_page_size;
294         iface->render_pixbuf = dvi_document_render_pixbuf;
295         iface->get_info = dvi_document_get_info;
296         iface->get_orientation = dvi_document_get_orientation;
297         iface->set_orientation = dvi_document_set_orientation;
298 }
299
300 static void
301 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
302                                            gint                  page,
303                                            gint                  suggested_width,
304                                            gint                  *width,
305                                            gint                  *height)
306 {       
307         DviDocument *dvi_document = DVI_DOCUMENT (document); 
308         gdouble page_ratio;
309         
310         page_ratio = dvi_document->base_height / dvi_document->base_width;
311         *width = suggested_width;
312         *height = (gint) (suggested_width * page_ratio);
313
314         return;
315 }
316
317 static GdkPixbuf *
318 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
319                                        gint                      page,
320                                        gint                      width,
321                                        gboolean                  border)
322 {
323         DviDocument *dvi_document = DVI_DOCUMENT (document);
324         GdkPixbuf *pixbuf;
325         GdkPixbuf *border_pixbuf;
326         gint thumb_width, thumb_height;
327         gint proposed_width, proposed_height;
328         
329         dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
330
331         g_mutex_lock (dvi_context_mutex);
332
333         mdvi_setpage(dvi_document->context,  page);
334
335         mdvi_set_shrink (dvi_document->context, 
336                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
337                           (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
338
339         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
340         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
341                           
342         if (border) {
343                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
344                                                  MAX (thumb_width - proposed_width, 0) / 2,
345                                                  MAX (thumb_height - proposed_height, 0) / 2);  
346         } else {
347                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
348                                                  MAX (thumb_width - proposed_width - 2, 0) / 2,
349                                                  MAX (thumb_height - proposed_height - 2, 0) / 2);      
350         }
351         
352
353         mdvi_pixbuf_device_render (dvi_document->context);
354         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
355
356         g_mutex_unlock (dvi_context_mutex);
357
358         if (border) {
359                 border_pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
360                 gdk_pixbuf_copy_area (pixbuf, 0, 0, 
361                                       thumb_width - 2, thumb_height - 2,
362                                       border_pixbuf, 2, 2);
363                 g_object_unref (pixbuf);
364                 pixbuf = border_pixbuf;
365         }
366         
367         
368         return pixbuf;
369 }
370
371 static void
372 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
373 {
374         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
375         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
376 }
377
378
379 static void
380 dvi_document_init_params (DviDocument *dvi_document)
381 {       
382         dvi_document->params = g_new0 (DviParams, 1);   
383
384         dvi_document->params->dpi      = MDVI_DPI;
385         dvi_document->params->vdpi     = MDVI_VDPI;
386         dvi_document->params->mag      = MDVI_MAGNIFICATION;
387         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
388         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
389         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
390         dvi_document->params->hdrift   = 0;
391         dvi_document->params->vdrift   = 0;
392         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
393         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
394         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
395
396         dvi_document->spec = NULL;
397         
398         dvi_document->params->bg = 0xffffffff;
399         dvi_document->params->fg = 0xff000000;
400
401         mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
402         
403         mdvi_register_fonts ();
404 }
405
406 static void
407 dvi_document_init (DviDocument *dvi_document)
408 {
409         dvi_document->context = NULL;
410         dvi_document_init_params (dvi_document);
411 }