]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/dvi-document.c
Fix memory leak.
[evince.git] / backend / 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 <config.h>
23 #include "dvi-document.h"
24 #include "ev-document-thumbnails.h"
25 #include "ev-document-misc.h"
26 #include "ev-file-exporter.h"
27
28 #include "mdvi.h"
29 #include "fonts.h"
30 #include "cairo-device.h"
31
32 #include <glib/gi18n.h>
33
34 GMutex *dvi_context_mutex = NULL;
35
36 enum {
37         PROP_0,
38         PROP_TITLE
39 };
40
41 struct _DviDocumentClass
42 {
43         GObjectClass parent_class;
44 };
45
46 struct _DviDocument
47 {
48         GObject parent_instance;
49
50         DviContext *context;
51         DviPageSpec *spec;
52         DviParams *params;
53         
54         /* To let document scale we should remember width and height */
55         double base_width;
56         double base_height;
57         
58         gchar *uri;
59
60         /* PDF exporter */
61         gchar            *exporter_filename;
62         GString          *exporter_opts;
63 };
64
65 typedef struct _DviDocumentClass DviDocumentClass;
66
67 static void dvi_document_document_iface_init            (EvDocumentIface           *iface);
68 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
69 static void dvi_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
70 static void dvi_document_do_color_special               (DviContext                *dvi,
71                                                          const char                *prefix,
72                                                          const char                *arg);
73
74 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
75      {
76       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
77       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
78      });
79
80 static gboolean
81 dvi_document_load (EvDocument  *document,
82                    const char  *uri,
83                    GError     **error)
84 {
85         gchar *filename;
86         DviDocument *dvi_document = DVI_DOCUMENT(document);
87         
88         filename = g_filename_from_uri (uri, NULL, error);
89         
90         if (!filename) {
91                 g_set_error (error,
92                              EV_DOCUMENT_ERROR,
93                              EV_DOCUMENT_ERROR_INVALID,
94                              _("File not available"));
95                 return FALSE;
96         }
97         
98         g_mutex_lock (dvi_context_mutex);
99         if (dvi_document->context)
100                 mdvi_destroy_context (dvi_document->context);
101
102         dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
103         g_mutex_unlock (dvi_context_mutex);
104         g_free (filename);
105         
106         if (!dvi_document->context) {
107                 g_set_error (error,
108                              EV_DOCUMENT_ERROR,
109                              EV_DOCUMENT_ERROR_INVALID,
110                              _("DVI document has incorrect format"));
111                 return FALSE;
112         }
113         
114         mdvi_cairo_device_init (&dvi_document->context->device);
115         
116         
117         dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv 
118                 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
119         
120         dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv 
121                 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
122         
123         g_free (dvi_document->uri);
124         dvi_document->uri = g_strdup (uri);
125         
126         return TRUE;
127 }
128
129
130 static gboolean
131 dvi_document_save (EvDocument  *document,
132                       const char  *uri,
133                       GError     **error)
134 {
135         DviDocument *dvi_document = DVI_DOCUMENT (document);
136
137         return ev_xfer_uri_simple (dvi_document->uri, uri, error);
138 }
139
140 static int
141 dvi_document_get_n_pages (EvDocument *document)
142 {
143         DviDocument *dvi_document = DVI_DOCUMENT (document);
144         
145         return dvi_document->context->npages;
146 }
147
148 static void
149 dvi_document_get_page_size (EvDocument *document,
150                             EvPage     *page,
151                             double     *width,
152                             double     *height)
153 {
154         DviDocument *dvi_document = DVI_DOCUMENT (document);    
155
156         *width = dvi_document->base_width;
157         *height = dvi_document->base_height;;
158 }
159
160 static cairo_surface_t *
161 dvi_document_render (EvDocument      *document,
162                      EvRenderContext *rc)
163 {
164         cairo_surface_t *surface;
165         cairo_surface_t *rotated_surface;
166         DviDocument *dvi_document = DVI_DOCUMENT(document);
167         gint required_width, required_height;
168         gint proposed_width, proposed_height;
169         gint xmargin = 0, ymargin = 0;
170
171         /* We should protect our context since it's not 
172          * thread safe. The work to the future - 
173          * let context render page independently
174          */
175         g_mutex_lock (dvi_context_mutex);
176         
177         mdvi_setpage (dvi_document->context, rc->page->index);
178         
179         mdvi_set_shrink (dvi_document->context, 
180                          (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
181                          (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
182
183         required_width = dvi_document->base_width * rc->scale + 0.5;
184         required_height = dvi_document->base_height * rc->scale + 0.5;
185         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
186         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
187         
188         if (required_width >= proposed_width)
189             xmargin = (required_width - proposed_width) / 2;
190         if (required_height >= proposed_height)
191             ymargin = (required_height - proposed_height) / 2;
192             
193         mdvi_cairo_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
194         mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
195         mdvi_cairo_device_render (dvi_document->context);
196         surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
197
198         g_mutex_unlock (dvi_context_mutex);
199
200         rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
201                                                                      required_width,
202                                                                      required_height, 
203                                                                      rc->rotation);
204         cairo_surface_destroy (surface);
205         
206         return rotated_surface;
207 }
208
209 static void
210 dvi_document_finalize (GObject *object)
211 {       
212         DviDocument *dvi_document = DVI_DOCUMENT(object);
213         
214         g_mutex_lock (dvi_context_mutex);
215         if (dvi_document->context) {
216                 mdvi_cairo_device_free (&dvi_document->context->device);
217                 mdvi_destroy_context (dvi_document->context);
218         }
219         g_mutex_unlock (dvi_context_mutex);
220
221         if (dvi_document->params)
222                 g_free (dvi_document->params);
223
224         if (dvi_document->exporter_filename)
225                 g_free (dvi_document->exporter_filename);
226         
227         if (dvi_document->exporter_opts)
228                 g_string_free (dvi_document->exporter_opts, TRUE);
229
230         g_free (dvi_document->uri);
231                 
232         G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
233 }
234
235
236 static void
237 dvi_document_class_init (DviDocumentClass *klass)
238 {
239         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
240
241         gobject_class->finalize = dvi_document_finalize;
242
243         mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
244         mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
245         mdvi_register_fonts ();
246
247         dvi_context_mutex = g_mutex_new ();
248 }
249
250 static EvDocumentInfo *
251 dvi_document_get_info (EvDocument *document)
252 {
253         EvDocumentInfo *info;
254
255         info = g_new0 (EvDocumentInfo, 1);
256
257         return info;
258 }
259
260 static void
261 dvi_document_document_iface_init (EvDocumentIface *iface)
262 {
263         iface->load = dvi_document_load;
264         iface->save = dvi_document_save;
265         iface->get_n_pages = dvi_document_get_n_pages;
266         iface->get_page_size = dvi_document_get_page_size;
267         iface->render = dvi_document_render;
268         iface->get_info = dvi_document_get_info;
269 }
270
271 static void
272 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
273                                         EvRenderContext      *rc, 
274                                         gint                 *width,
275                                         gint                 *height)
276 {       
277         DviDocument *dvi_document = DVI_DOCUMENT (document);
278         gdouble page_width = dvi_document->base_width;
279         gdouble page_height = dvi_document->base_height;
280
281         if (rc->rotation == 90 || rc->rotation == 270) {
282                 *width = (gint) (page_height * rc->scale);
283                 *height = (gint) (page_width * rc->scale);
284         } else {
285                 *width = (gint) (page_width * rc->scale);
286                 *height = (gint) (page_height * rc->scale);
287         }
288 }
289
290 static GdkPixbuf *
291 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
292                                        EvRenderContext      *rc,   
293                                        gboolean              border)
294 {
295         DviDocument *dvi_document = DVI_DOCUMENT (document);
296         GdkPixbuf *pixbuf;
297         GdkPixbuf *rotated_pixbuf;
298         cairo_surface_t *surface;
299         gint thumb_width, thumb_height;
300         gint proposed_width, proposed_height;
301
302         thumb_width = (gint) (dvi_document->base_width * rc->scale);
303         thumb_height = (gint) (dvi_document->base_height * rc->scale);
304
305         g_mutex_lock (dvi_context_mutex);
306         
307         mdvi_setpage (dvi_document->context, rc->page->index);
308
309         mdvi_set_shrink (dvi_document->context, 
310                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
311                           (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
312
313         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
314         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
315                           
316         if (border) {
317                 mdvi_cairo_device_set_margins (&dvi_document->context->device, 
318                                                MAX (thumb_width - proposed_width, 0) / 2,
319                                                MAX (thumb_height - proposed_height, 0) / 2);    
320         } else {
321                 mdvi_cairo_device_set_margins (&dvi_document->context->device, 
322                                                MAX (thumb_width - proposed_width - 2, 0) / 2,
323                                                MAX (thumb_height - proposed_height - 2, 0) / 2);        
324         }
325
326         mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
327         mdvi_cairo_device_render (dvi_document->context);
328         surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
329         g_mutex_unlock (dvi_context_mutex);
330
331         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
332         cairo_surface_destroy (surface);
333
334         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
335         g_object_unref (pixbuf);
336
337         if (border) {
338                 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
339
340                 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
341                 g_object_unref (tmp_pixbuf);
342         }
343
344         return rotated_pixbuf;
345 }
346
347 static void
348 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
349 {
350         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
351         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
352 }
353
354 /* EvFileExporterIface */
355 static void
356 dvi_document_file_exporter_begin (EvFileExporter        *exporter,
357                                   EvFileExporterContext *fc)
358 {
359         DviDocument *dvi_document = DVI_DOCUMENT(exporter);
360         
361         if (dvi_document->exporter_filename)
362                 g_free (dvi_document->exporter_filename);       
363         dvi_document->exporter_filename = g_strdup (fc->filename);
364         
365         if (dvi_document->exporter_opts) {
366                 g_string_free (dvi_document->exporter_opts, TRUE);
367         }
368         dvi_document->exporter_opts = g_string_new ("-s ");
369 }
370
371 static void
372 dvi_document_file_exporter_do_page (EvFileExporter  *exporter,
373                                     EvRenderContext *rc)
374 {
375        DviDocument *dvi_document = DVI_DOCUMENT(exporter);
376
377        g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
378 }
379
380 static void
381 dvi_document_file_exporter_end (EvFileExporter *exporter)
382 {
383         gchar *command_line;
384         gint exit_stat;
385         GError *err = NULL;
386         gboolean success;
387         
388         DviDocument *dvi_document = DVI_DOCUMENT(exporter);
389         
390         command_line = g_strdup_printf ("dvipdfm %s -o %s \"%s\"", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
391                                         dvi_document->exporter_opts->str,
392                                         dvi_document->exporter_filename,
393                                         dvi_document->context->filename);
394         
395         success = g_spawn_command_line_sync (command_line,
396                                              NULL,
397                                              NULL,
398                                              &exit_stat,
399                                              &err);
400
401         g_free (command_line);
402
403         if (success == FALSE) {
404                 g_warning ("Error: %s", err->message);
405         } else if (exit_stat != 0) {
406                 g_warning ("Error: dvipdfm exited with non-zero status.");
407         }
408
409         if (err)
410                 g_error_free (err);
411 }
412
413 static EvFileExporterCapabilities
414 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
415 {
416         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
417                 EV_FILE_EXPORTER_CAN_COPIES |
418                 EV_FILE_EXPORTER_CAN_COLLATE |
419                 EV_FILE_EXPORTER_CAN_REVERSE |
420                 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
421 }
422
423 static void
424 dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
425 {
426         iface->begin = dvi_document_file_exporter_begin;
427         iface->do_page = dvi_document_file_exporter_do_page;
428         iface->end = dvi_document_file_exporter_end;
429         iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
430 }
431
432 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
433
434 static gboolean
435 hsb2rgb (float h, float s, float v, char *red, char *green, char *blue)
436 {
437         float i, f, p, q, t, r, g, b;
438
439         if (h == 360)
440                 h = 0;
441         else if ((h > 360) || (h < 0))
442                 return FALSE;
443
444         s /= 100;
445         v /= 100;
446         h /= 60;
447         i = floor (h);
448         f = h - i;
449         p = v * (1 - s);
450         q = v * (1 - (s * f));
451         t = v * (1 - (s * (1 - f)));
452
453         if (i == 0) {
454                 r = v;
455                 g = t;
456                 b = p;
457         } else if (i == 1) {
458                 r = q;
459                 g = v;
460                 b = p;
461         } else if (i == 2) {
462                 r = p;
463                 g = v;
464                 b = t;
465         } else if (i == 3) {
466                 r = p;
467                 g = q;
468                 b = v;
469         } else if (i == 4) {
470                 r = t;
471                 g = p;
472                 b = v;
473         } else if (i == 5) {
474                 r = v;
475                 g = p;
476                 b = q;
477         }
478
479         (*red) = (char)floor(r * 255);
480         (*green) = (char)floor(g * 255);
481         (*blue) = (char)floor(b * 255);
482         
483         return TRUE;
484 }
485
486 static void
487 parse_color (const gchar *ptr,
488              gdouble     *color,
489              gint         n_color)
490 {
491         gchar *p = (gchar *)ptr;
492         gint   i;
493
494         for (i = 0; i < n_color; i++) {
495                 while (isspace (*p)) p++;
496                 color[i] = g_ascii_strtod (p, NULL);
497                 while (!isspace (*p) && *p != '\0') p++;
498                 if (*p == '\0')
499                         break;
500         }
501 }
502
503 static void
504 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
505 {
506         if (strncmp (arg, "pop", 3) == 0) {
507                 mdvi_pop_color (dvi);
508         } else if (strncmp (arg, "push", 4) == 0) {
509                 /* Find color source: Named, CMYK or RGB */
510                 const char *tmp = arg + 4;
511                 
512                 while (isspace (*tmp)) tmp++;
513
514                 if (!strncmp ("rgb", tmp, 3)) {
515                         gdouble rgb[3];
516                         guchar red, green, blue;
517
518                         parse_color (tmp + 4, rgb, 3);
519                         
520                         red = 255 * rgb[0];
521                         green = 255 * rgb[1];
522                         blue = 255 * rgb[2];
523
524                         mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
525                 } else if (!strncmp ("hsb", tmp, 4)) {
526                         gdouble hsb[3];
527                         guchar red, green, blue;
528
529                         parse_color (tmp + 4, hsb, 3);
530                         
531                         if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
532                                 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
533                 } else if (!strncmp ("cmyk", tmp, 4)) {
534                         gdouble cmyk[4];
535                         double r, g, b;
536                         guchar red, green, blue;
537                         
538                         parse_color (tmp + 5, cmyk, 4);
539
540                         r = 1.0 - cmyk[0] - cmyk[3];
541                         if (r < 0.0)
542                                 r = 0.0;
543                         g = 1.0 - cmyk[1] - cmyk[3];
544                         if (g < 0.0)
545                                 g = 0.0;
546                         b = 1.0 - cmyk[2] - cmyk[3];
547                         if (b < 0.0)
548                                 b = 0.0;
549
550                         red = r * 255 + 0.5;
551                         green = g * 255 + 0.5;
552                         blue = b * 255 + 0.5;
553                         
554                         mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
555                 } else if (!strncmp ("gray ", tmp, 5)) {
556                         gdouble gray;
557                         guchar rgb;
558
559                         parse_color (tmp + 5, &gray, 1);
560
561                         rgb = gray * 255 + 0.5;
562
563                         mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
564                 } else {
565                         GdkColor color;
566                         
567                         if (gdk_color_parse (tmp, &color)) {
568                                 guchar red, green, blue;
569
570                                 red = color.red * 255 / 65535.;
571                                 green = color.green * 255 / 65535.;
572                                 blue = color.blue * 255 / 65535.;
573
574                                 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
575                         }
576                 }
577         }
578 }
579
580 static void
581 dvi_document_init_params (DviDocument *dvi_document)
582 {       
583         dvi_document->params = g_new0 (DviParams, 1);   
584
585         dvi_document->params->dpi      = MDVI_DPI;
586         dvi_document->params->vdpi     = MDVI_VDPI;
587         dvi_document->params->mag      = MDVI_MAGNIFICATION;
588         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
589         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
590         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
591         dvi_document->params->hdrift   = 0;
592         dvi_document->params->vdrift   = 0;
593         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
594         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
595         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
596
597         dvi_document->spec = NULL;
598         
599         dvi_document->params->bg = 0xffffffff;
600         dvi_document->params->fg = 0xff000000;
601 }
602
603 static void
604 dvi_document_init (DviDocument *dvi_document)
605 {
606         dvi_document->context = NULL;
607         dvi_document_init_params (dvi_document);
608
609         dvi_document->exporter_filename = NULL;
610         dvi_document->exporter_opts = NULL;
611 }