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