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