]> www.fi.muni.cz Git - evince.git/blob - dvi/dvi-document.c
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         gchar *uri;
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     g_free (dvi_document->uri);
119     dvi_document->uri = g_strdup (uri);
120
121     return TRUE;
122 }
123
124
125 static gboolean
126 dvi_document_save (EvDocument  *document,
127                       const char  *uri,
128                       GError     **error)
129 {
130         DviDocument *dvi_document = DVI_DOCUMENT (document);
131
132         return ev_xfer_uri_simple (dvi_document->uri, uri, error);
133 }
134
135 static int
136 dvi_document_get_n_pages (EvDocument  *document)
137 {
138     DviDocument *dvi_document = DVI_DOCUMENT (document);
139     return dvi_document->context->npages;
140 }
141
142 static void
143 dvi_document_get_page_size (EvDocument   *document,
144                             int       page,
145                             double    *width,
146                             double    *height)
147 {
148         DviDocument * dvi_document = DVI_DOCUMENT (document);   
149
150         *width = dvi_document->base_width;
151         *height = dvi_document->base_height;;
152                                     
153         return;
154 }
155
156 static GdkPixbuf *
157 dvi_document_render_pixbuf (EvDocument  *document,
158                             EvRenderContext *rc)
159 {
160         GdkPixbuf *pixbuf;
161         GdkPixbuf *rotated_pixbuf;
162
163         DviDocument *dvi_document = DVI_DOCUMENT(document);
164
165         gint required_width, required_height;
166         gint proposed_width, proposed_height;
167         gint xmargin = 0, ymargin = 0;
168
169         /* We should protect our context since it's not 
170          * thread safe. The work to the future - 
171          * let context render page independently
172          */
173         g_mutex_lock (dvi_context_mutex);
174         
175         mdvi_setpage(dvi_document->context,  rc->page);
176         
177         mdvi_set_shrink (dvi_document->context, 
178                          (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
179                          (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
180
181         required_width = dvi_document->base_width * rc->scale;
182         required_height = dvi_document->base_height * rc->scale;
183         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
184         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
185         
186         if (required_width >= proposed_width)
187             xmargin = (required_width - proposed_width) / 2;
188         if (required_height >= proposed_height)
189             ymargin = (required_height - proposed_height) / 2;
190             
191         mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
192
193         mdvi_pixbuf_device_render (dvi_document->context);
194         
195         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
196
197         g_mutex_unlock (dvi_context_mutex);
198
199         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
200         g_object_unref (pixbuf);
201
202         return rotated_pixbuf;
203 }
204
205 static void
206 dvi_document_finalize (GObject *object)
207 {       
208         DviDocument *dvi_document = DVI_DOCUMENT(object);
209
210         if (dvi_document->context)
211             {
212                 mdvi_pixbuf_device_free (&dvi_document->context->device);
213                 mdvi_destroy_context (dvi_document->context);
214             }
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
233 static gboolean
234 dvi_document_can_get_text (EvDocument *document)
235 {
236         return FALSE;
237 }
238
239 static EvDocumentInfo *
240 dvi_document_get_info (EvDocument *document)
241 {
242         EvDocumentInfo *info;
243
244         info = g_new0 (EvDocumentInfo, 1);
245
246         return info;
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->can_get_text = dvi_document_can_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         iface->get_info = dvi_document_get_info;
259 }
260
261 static void
262 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
263                                         gint                  page,
264                                         gint                  suggested_width,
265                                         gint                  *width,
266                                         gint                  *height)
267 {       
268         DviDocument *dvi_document = DVI_DOCUMENT (document); 
269         gdouble page_ratio;
270         
271         page_ratio = dvi_document->base_height / dvi_document->base_width;
272         *width = suggested_width;
273         *height = (gint) (suggested_width * page_ratio);
274
275         return;
276 }
277
278 static GdkPixbuf *
279 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
280                                        gint                      page,
281                                        gint                      rotation,
282                                        gint                      width,
283                                        gboolean                  border)
284 {
285         DviDocument *dvi_document = DVI_DOCUMENT (document);
286         GdkPixbuf *pixbuf;
287         GdkPixbuf *border_pixbuf;
288         GdkPixbuf *rotated_pixbuf;
289         gint thumb_width, thumb_height;
290         gint proposed_width, proposed_height;
291         
292         dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
293
294         g_mutex_lock (dvi_context_mutex);
295
296         mdvi_setpage(dvi_document->context,  page);
297
298         mdvi_set_shrink (dvi_document->context, 
299                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
300                           (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
301
302         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
303         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
304                           
305         if (border) {
306                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
307                                                  MAX (thumb_width - proposed_width, 0) / 2,
308                                                  MAX (thumb_height - proposed_height, 0) / 2);  
309         } else {
310                 mdvi_pixbuf_device_set_margins  (&dvi_document->context->device, 
311                                                  MAX (thumb_width - proposed_width - 2, 0) / 2,
312                                                  MAX (thumb_height - proposed_height - 2, 0) / 2);      
313         }
314         
315
316         mdvi_pixbuf_device_render (dvi_document->context);
317         pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
318
319         g_mutex_unlock (dvi_context_mutex);
320         
321         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation);
322         g_object_unref (pixbuf);
323         
324         if (border) {
325               GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
326               rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
327               g_object_unref (tmp_pixbuf);
328         }
329
330         return rotated_pixbuf;
331 }
332
333 static void
334 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
335 {
336         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
337         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
338 }
339
340
341 static void
342 dvi_document_init_params (DviDocument *dvi_document)
343 {       
344         dvi_document->params = g_new0 (DviParams, 1);   
345
346         dvi_document->params->dpi      = MDVI_DPI;
347         dvi_document->params->vdpi     = MDVI_VDPI;
348         dvi_document->params->mag      = MDVI_MAGNIFICATION;
349         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
350         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
351         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
352         dvi_document->params->hdrift   = 0;
353         dvi_document->params->vdrift   = 0;
354         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
355         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
356         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
357
358         dvi_document->spec = NULL;
359         
360         dvi_document->params->bg = 0xffffffff;
361         dvi_document->params->fg = 0xff000000;
362
363         mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
364         
365         mdvi_register_fonts ();
366 }
367
368 static void
369 dvi_document_init (DviDocument *dvi_document)
370 {
371         dvi_document->context = NULL;
372         dvi_document_init_params (dvi_document);
373 }