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