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