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