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