]> www.fi.muni.cz Git - evince.git/blob - dvi/dvi-document.c
Added tr to ALL_LINGUAS
[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         *width = dvi_document->base_width;
146         *height = dvi_document->base_height;;
147                                     
148         return;
149 }
150
151 static GdkPixbuf *
152 dvi_document_render_pixbuf (EvDocument  *document,
153                             EvRenderContext *rc)
154 {
155         GdkPixbuf *pixbuf;
156         GdkPixbuf *rotated_pixbuf;
157
158         DviDocument *dvi_document = DVI_DOCUMENT(document);
159
160         gint required_width, required_height;
161         gint proposed_width, proposed_height;
162         gint xmargin = 0, ymargin = 0;
163
164         /* We should protect our context since it's not 
165          * thread safe. The work to the future - 
166          * let context render page independently
167          */
168         g_mutex_lock (dvi_context_mutex);
169         
170         mdvi_setpage(dvi_document->context,  rc->page);
171         
172         mdvi_set_shrink (dvi_document->context, 
173                          (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
174                          (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
175
176         required_width = dvi_document->base_width * rc->scale;
177         required_height = dvi_document->base_height * rc->scale;
178         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
179         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
180         
181         if (required_width >= proposed_width)
182             xmargin = (required_width - proposed_width) / 2;
183         if (required_height >= proposed_height)
184             ymargin = (required_height - proposed_height) / 2;
185             
186         mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
187
188         mdvi_pixbuf_device_render (dvi_document->context);
189         
190         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
191
192         g_mutex_unlock (dvi_context_mutex);
193
194         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, rc->rotation);
195         g_object_unref (pixbuf);
196
197         return rotated_pixbuf;
198 }
199
200 static void
201 dvi_document_finalize (GObject *object)
202 {       
203         DviDocument *dvi_document = DVI_DOCUMENT(object);
204
205         if (dvi_document->context)
206             {
207                 mdvi_pixbuf_device_free (&dvi_document->context->device);
208                 mdvi_destroy_context (dvi_document->context);
209             }
210
211         if (dvi_document->params)
212                 g_free (dvi_document->params);
213                 
214         G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
215 }
216
217
218 static void
219 dvi_document_class_init (DviDocumentClass *klass)
220 {
221         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
222
223         gobject_class->finalize = dvi_document_finalize;
224 }
225
226 static gboolean
227 dvi_document_can_get_text (EvDocument *document)
228 {
229         return FALSE;
230 }
231
232 static EvDocumentInfo *
233 dvi_document_get_info (EvDocument *document)
234 {
235         EvDocumentInfo *info;
236
237         info = g_new0 (EvDocumentInfo, 1);
238
239         return info;
240 }
241
242 static void
243 dvi_document_document_iface_init (EvDocumentIface *iface)
244 {
245         iface->load = dvi_document_load;
246         iface->save = dvi_document_save;
247         iface->can_get_text = dvi_document_can_get_text;
248         iface->get_n_pages = dvi_document_get_n_pages;
249         iface->get_page_size = dvi_document_get_page_size;
250         iface->render_pixbuf = dvi_document_render_pixbuf;
251         iface->get_info = dvi_document_get_info;
252 }
253
254 static void
255 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
256                                         gint                  page,
257                                         gint                  suggested_width,
258                                         gint                  *width,
259                                         gint                  *height)
260 {       
261         DviDocument *dvi_document = DVI_DOCUMENT (document); 
262         gdouble page_ratio;
263         
264         page_ratio = dvi_document->base_height / dvi_document->base_width;
265         *width = suggested_width;
266         *height = (gint) (suggested_width * page_ratio);
267
268         return;
269 }
270
271 static GdkPixbuf *
272 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
273                                        gint                      page,
274                                        gint                      rotation,
275                                        gint                      width,
276                                        gboolean                  border)
277 {
278         DviDocument *dvi_document = DVI_DOCUMENT (document);
279         GdkPixbuf *pixbuf;
280         GdkPixbuf *border_pixbuf;
281         GdkPixbuf *rotated_pixbuf;
282         gint thumb_width, thumb_height;
283         gint proposed_width, proposed_height;
284         
285         dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
286
287         g_mutex_lock (dvi_context_mutex);
288
289         mdvi_setpage(dvi_document->context,  page);
290
291         mdvi_set_shrink (dvi_document->context, 
292                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
293                           (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
294
295         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
296         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
297                           
298         if (border) {
299                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
300                                                  MAX (thumb_width - proposed_width, 0) / 2,
301                                                  MAX (thumb_height - proposed_height, 0) / 2);  
302         } else {
303                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
304                                                  MAX (thumb_width - proposed_width - 2, 0) / 2,
305                                                  MAX (thumb_height - proposed_height - 2, 0) / 2);      
306         }
307         
308
309         mdvi_pixbuf_device_render (dvi_document->context);
310         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
311
312         g_mutex_unlock (dvi_context_mutex);
313
314         if (border) {
315                 border_pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
316                 gdk_pixbuf_copy_area (pixbuf, 0, 0, 
317                                       thumb_width - 2, thumb_height - 2,
318                                       border_pixbuf, 2, 2);
319                 g_object_unref (pixbuf);
320                 pixbuf = border_pixbuf;
321         }
322         
323         
324         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, rotation);
325         g_object_unref (pixbuf);
326
327         return rotated_pixbuf;
328 }
329
330 static void
331 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
332 {
333         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
334         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
335 }
336
337
338 static void
339 dvi_document_init_params (DviDocument *dvi_document)
340 {       
341         dvi_document->params = g_new0 (DviParams, 1);   
342
343         dvi_document->params->dpi      = MDVI_DPI;
344         dvi_document->params->vdpi     = MDVI_VDPI;
345         dvi_document->params->mag      = MDVI_MAGNIFICATION;
346         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
347         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
348         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
349         dvi_document->params->hdrift   = 0;
350         dvi_document->params->vdrift   = 0;
351         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
352         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
353         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
354
355         dvi_document->spec = NULL;
356         
357         dvi_document->params->bg = 0xffffffff;
358         dvi_document->params->fg = 0xff000000;
359
360         mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
361         
362         mdvi_register_fonts ();
363 }
364
365 static void
366 dvi_document_init (DviDocument *dvi_document)
367 {
368         dvi_document->context = NULL;
369         dvi_document_init_params (dvi_document);
370 }