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