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