]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/dvi-document.c
56a0d9231d3f214f1c40cc2c9dfc13a5044458bd
[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
27 #include "mdvi.h"
28 #include "fonts.h"
29 #include "cairo-device.h"
30
31 #include <glib/gi18n.h>
32
33 GMutex *dvi_context_mutex = NULL;
34
35 enum {
36         PROP_0,
37         PROP_TITLE
38 };
39
40 struct _DviDocumentClass
41 {
42         GObjectClass parent_class;
43 };
44
45 struct _DviDocument
46 {
47         GObject parent_instance;
48
49         DviContext *context;
50         DviPageSpec *spec;
51         DviParams *params;
52         
53         /* To let document scale we should remember width and height */
54         double base_width;
55         double base_height;
56         
57         gchar *uri;
58
59         /* PDF exporter */
60         gchar            *exporter_filename;
61         GString          *exporter_opts;
62 };
63
64 typedef struct _DviDocumentClass DviDocumentClass;
65
66 static void dvi_document_document_iface_init            (EvDocumentIface           *iface);
67 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
68 static void dvi_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
69 static void dvi_document_get_page_size                  (EvDocument                *document,
70                                                          int                        page,
71                                                          double                    *width,
72                                                          double                    *height);
73 static void dvi_document_do_color_special               (DviContext                *dvi,
74                                                          const char                *prefix,
75                                                          const char                *arg);
76
77 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
78      {
79       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
80       G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
81      });
82
83 static gboolean
84 dvi_document_load (EvDocument  *document,
85                    const char  *uri,
86                    GError     **error)
87 {
88         gchar *filename;
89         DviDocument *dvi_document = DVI_DOCUMENT(document);
90         
91         filename = g_filename_from_uri (uri, NULL, error);
92         
93         if (!filename) {
94                 g_set_error (error,
95                              EV_DOCUMENT_ERROR,
96                              EV_DOCUMENT_ERROR_INVALID,
97                              _("File not available"));
98                 return FALSE;
99         }
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         
108         if (!dvi_document->context) {
109                 g_set_error (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                             int         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);
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
238 static void
239 dvi_document_class_init (DviDocumentClass *klass)
240 {
241         GObjectClass *gobject_class = G_OBJECT_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
252 static EvDocumentInfo *
253 dvi_document_get_info (EvDocument *document)
254 {
255         EvDocumentInfo *info;
256
257         info = g_new0 (EvDocumentInfo, 1);
258
259         return info;
260 }
261
262 static void
263 dvi_document_document_iface_init (EvDocumentIface *iface)
264 {
265         iface->load = dvi_document_load;
266         iface->save = dvi_document_save;
267         iface->get_n_pages = dvi_document_get_n_pages;
268         iface->get_page_size = dvi_document_get_page_size;
269         iface->render = dvi_document_render;
270         iface->get_info = dvi_document_get_info;
271 }
272
273 static void
274 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
275                                         EvRenderContext      *rc, 
276                                         gint                  *width,
277                                         gint                  *height)
278 {       
279         DviDocument *dvi_document = DVI_DOCUMENT (document);
280         gdouble page_width = dvi_document->base_width;
281         gdouble page_height = dvi_document->base_height;
282
283         if (rc->rotation == 90 || rc->rotation == 270) {
284                 *width = (gint) (page_height * rc->scale);
285                 *height = (gint) (page_width * rc->scale);
286         } else {
287                 *width = (gint) (page_width * rc->scale);
288                 *height = (gint) (page_height * rc->scale);
289         }
290 }
291
292 static GdkPixbuf *
293 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
294                                        EvRenderContext      *rc,   
295                                        gboolean              border)
296 {
297         DviDocument *dvi_document = DVI_DOCUMENT (document);
298         GdkPixbuf *pixbuf;
299         GdkPixbuf *rotated_pixbuf;
300         cairo_surface_t *surface;
301         gint thumb_width, thumb_height;
302         gint proposed_width, proposed_height;
303
304         thumb_width = (gint) (dvi_document->base_width * rc->scale);
305         thumb_height = (gint) (dvi_document->base_height * rc->scale);
306
307         g_mutex_lock (dvi_context_mutex);
308         
309         mdvi_setpage (dvi_document->context, rc->page);
310
311         mdvi_set_shrink (dvi_document->context, 
312                           (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
313                           (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
314
315         proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
316         proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
317                           
318         if (border) {
319                 mdvi_cairo_device_set_margins (&dvi_document->context->device, 
320                                                MAX (thumb_width - proposed_width, 0) / 2,
321                                                MAX (thumb_height - proposed_height, 0) / 2);    
322         } else {
323                 mdvi_cairo_device_set_margins (&dvi_document->context->device, 
324                                                MAX (thumb_width - proposed_width - 2, 0) / 2,
325                                                MAX (thumb_height - proposed_height - 2, 0) / 2);        
326         }
327
328         mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
329         mdvi_cairo_device_render (dvi_document->context);
330         surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
331         g_mutex_unlock (dvi_context_mutex);
332
333         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
334         cairo_surface_destroy (surface);
335
336         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
337         g_object_unref (pixbuf);
338
339         if (border) {
340                 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
341
342                 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
343                 g_object_unref (tmp_pixbuf);
344         }
345
346         return rotated_pixbuf;
347 }
348
349 static void
350 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
351 {
352         iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
353         iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
354 }
355
356 /* EvFileExporterIface */
357 static void
358 dvi_document_file_exporter_begin (EvFileExporter        *exporter,
359                                   EvFileExporterContext *fc)
360 {
361         DviDocument *dvi_document = DVI_DOCUMENT(exporter);
362         
363         if (dvi_document->exporter_filename)
364                 g_free (dvi_document->exporter_filename);       
365         dvi_document->exporter_filename = g_strdup(fc->filename);
366         
367         if (dvi_document->exporter_opts) {
368                 g_string_free (dvi_document->exporter_opts, TRUE);
369         }
370         dvi_document->exporter_opts = g_string_new ("-s ");
371 }
372
373 static void
374 dvi_document_file_exporter_do_page (EvFileExporter  *exporter,
375                                     EvRenderContext *rc)
376 {
377        DviDocument *dvi_document = DVI_DOCUMENT(exporter);
378
379        g_string_append_printf(dvi_document->exporter_opts, "%d,", (rc->page)+1);
380 }
381
382 static void
383 dvi_document_file_exporter_end (EvFileExporter *exporter)
384 {
385         gchar *command_line;
386         gint exit_stat;
387         GError *err = NULL;
388         gboolean success;
389         
390         DviDocument *dvi_document = DVI_DOCUMENT(exporter);
391         
392         command_line = g_strdup_printf ("dvipdfm %s -o %s %s", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
393                                         dvi_document->exporter_opts->str,
394                                         dvi_document->exporter_filename,
395                                         dvi_document->context->filename);
396         
397         success = g_spawn_command_line_sync (command_line,
398                                              NULL,
399                                              NULL,
400                                              &exit_stat,
401                                              &err);
402
403         g_free(command_line);
404
405         if (success == FALSE) {
406                 g_warning ("Error: %s", err->message);
407         } else if (exit_stat != 0) {
408                 g_warning ("Error: dvipdfm exited with non-zero status.");
409         }
410
411         if (err)
412                 g_error_free(err);
413 }
414
415 static EvFileExporterCapabilities
416 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
417 {
418         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
419                 EV_FILE_EXPORTER_CAN_COPIES |
420                 EV_FILE_EXPORTER_CAN_COLLATE |
421                 EV_FILE_EXPORTER_CAN_REVERSE |
422                 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
423 }
424
425 static void
426 dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
427 {
428         iface->begin = dvi_document_file_exporter_begin;
429         iface->do_page = dvi_document_file_exporter_do_page;
430         iface->end = dvi_document_file_exporter_end;
431         iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
432 }
433
434 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
435
436 static gboolean
437 hsb2rgb (float h, float s, float v, char *red, char *green, char *blue)
438 {
439         float i, f, p, q, t, r, g, b;
440
441         if (h == 360)
442                 h = 0;
443         else if ((h > 360) || (h < 0))
444                 return FALSE;
445
446         s /= 100;
447         v /= 100;
448         h /= 60;
449         i = floor (h);
450         f = h - i;
451         p = v * (1 - s);
452         q = v * (1 - (s * f));
453         t = v * (1 - (s * (1 - f)));
454
455         if (i == 0) {
456                 r = v;
457                 g = t;
458                 b = p;
459         } else if (i == 1) {
460                 r = q;
461                 g = v;
462                 b = p;
463         } else if (i == 2) {
464                 r = p;
465                 g = v;
466                 b = t;
467         } else if (i == 3) {
468                 r = p;
469                 g = q;
470                 b = v;
471         } else if (i == 4) {
472                 r = t;
473                 g = p;
474                 b = v;
475         } else if (i == 5) {
476                 r = v;
477                 g = p;
478                 b = q;
479         }
480
481         (*red) = (char)floor(r * 255);
482         (*green) = (char)floor(g * 255);
483         (*blue) = (char)floor(b * 255);
484         
485         return TRUE;
486 }
487
488 static void
489 parse_color (const gchar *ptr,
490              gdouble     *color,
491              gint         n_color)
492 {
493         gchar *p = (gchar *)ptr;
494         gint   i;
495
496         for (i = 0; i < n_color; i++) {
497                 while (isspace (*p)) p++;
498                 color[i] = g_ascii_strtod (p, NULL);
499                 while (!isspace (*p) && *p != '\0') p++;
500                 if (*p == '\0')
501                         break;
502         }
503 }
504
505 static void
506 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
507 {
508         if (strncmp (arg, "pop", 3) == 0) {
509                 mdvi_pop_color (dvi);
510         } else if (strncmp (arg, "push", 4) == 0) {
511                 /* Find color source: Named, CMYK or RGB */
512                 const char *tmp = arg + 4;
513                 
514                 while (isspace (*tmp)) tmp++;
515
516                 if (!strncmp ("rgb", tmp, 3)) {
517                         gdouble rgb[3];
518                         guchar red, green, blue;
519
520                         parse_color (tmp + 4, rgb, 3);
521                         
522                         red = 255 * rgb[0];
523                         green = 255 * rgb[1];
524                         blue = 255 * rgb[2];
525
526                         mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
527                 } else if (!strncmp ("hsb", tmp, 4)) {
528                         gdouble hsb[3];
529                         guchar red, green, blue;
530
531                         parse_color (tmp + 4, hsb, 3);
532                         
533                         if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
534                                 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
535                 } else if (!strncmp ("cmyk", tmp, 4)) {
536                         gdouble cmyk[4];
537                         double r, g, b;
538                         guchar red, green, blue;
539                         
540                         parse_color (tmp + 5, cmyk, 4);
541
542                         r = 1.0 - cmyk[0] - cmyk[3];
543                         if (r < 0.0)
544                                 r = 0.0;
545                         g = 1.0 - cmyk[1] - cmyk[3];
546                         if (g < 0.0)
547                                 g = 0.0;
548                         b = 1.0 - cmyk[2] - cmyk[3];
549                         if (b < 0.0)
550                                 b = 0.0;
551
552                         red = r * 255 + 0.5;
553                         green = g * 255 + 0.5;
554                         blue = b * 255 + 0.5;
555                         
556                         mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
557                 } else if (!strncmp ("gray ", tmp, 5)) {
558                         gdouble gray;
559                         guchar rgb;
560
561                         parse_color (tmp + 5, &gray, 1);
562
563                         rgb = gray * 255 + 0.5;
564
565                         mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
566                 } else {
567                         GdkColor color;
568                         
569                         if (gdk_color_parse (tmp, &color)) {
570                                 guchar red, green, blue;
571
572                                 red = color.red * 255 / 65535.;
573                                 green = color.green * 255 / 65535.;
574                                 blue = color.blue * 255 / 65535.;
575
576                                 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
577                         }
578                 }
579         }
580 }
581
582 static void
583 dvi_document_init_params (DviDocument *dvi_document)
584 {       
585         dvi_document->params = g_new0 (DviParams, 1);   
586
587         dvi_document->params->dpi      = MDVI_DPI;
588         dvi_document->params->vdpi     = MDVI_VDPI;
589         dvi_document->params->mag      = MDVI_MAGNIFICATION;
590         dvi_document->params->density  = MDVI_DEFAULT_DENSITY;
591         dvi_document->params->gamma    = MDVI_DEFAULT_GAMMA;
592         dvi_document->params->flags    = MDVI_PARAM_ANTIALIASED;
593         dvi_document->params->hdrift   = 0;
594         dvi_document->params->vdrift   = 0;
595         dvi_document->params->hshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
596         dvi_document->params->vshrink  =  MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
597         dvi_document->params->orientation = MDVI_ORIENT_TBLR;
598
599         dvi_document->spec = NULL;
600         
601         dvi_document->params->bg = 0xffffffff;
602         dvi_document->params->fg = 0xff000000;
603 }
604
605 static void
606 dvi_document_init (DviDocument *dvi_document)
607 {
608         dvi_document->context = NULL;
609         dvi_document_init_params (dvi_document);
610
611         dvi_document->exporter_filename = NULL;
612         dvi_document->exporter_opts = NULL;
613 }