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