Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dvi-document.c
Go to the documentation of this file.
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 static GMutex dvi_context_mutex;
44 
45 enum {
48 };
49 
51 {
53 };
54 
56 {
58 
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 */
71  GString *exporter_opts;
72 };
73 
75 
78  const char *prefix,
79  const char *arg);
80 
82  {
84  });
85 
86 static gboolean
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,
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
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
142 {
143  DviDocument *dvi_document = DVI_DOCUMENT (document);
144 
145  return dvi_document->context->npages;
146 }
147 
148 static void
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 *
162  EvRenderContext *rc)
163 {
164  cairo_surface_t *surface;
165  cairo_surface_t *rotated_surface;
166  DviDocument *dvi_document = DVI_DOCUMENT(document);
167  gdouble xscale, yscale;
168  gint required_width, required_height;
169  gint proposed_width, proposed_height;
170  gint xmargin = 0, ymargin = 0;
171 
172  /* We should protect our context since it's not
173  * thread safe. The work to the future -
174  * let context render page independently
175  */
176  g_mutex_lock (&dvi_context_mutex);
177 
178  mdvi_setpage (dvi_document->context, rc->page->index);
179 
180  ev_render_context_compute_scales (rc, dvi_document->base_width, dvi_document->base_height,
181  &xscale, &yscale);
182  mdvi_set_shrink (dvi_document->context,
183  (int)((dvi_document->params->hshrink - 1) / xscale) + 1,
184  (int)((dvi_document->params->vshrink - 1) / yscale) + 1);
185 
186  ev_render_context_compute_scaled_size (rc, dvi_document->base_width, dvi_document->base_height,
187  &required_width, &required_height);
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, xscale, yscale);
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 static gboolean
240 {
241  return TRUE;
242 }
243 
244 static void
246 {
247  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
248  EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
249  gchar *texmfcnf;
250 
251  gobject_class->finalize = dvi_document_finalize;
252 
253  texmfcnf = get_texmfcnf();
255  g_free(texmfcnf);
256 
257  mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
259 
260  ev_document_class->load = dvi_document_load;
261  ev_document_class->save = dvi_document_save;
262  ev_document_class->get_n_pages = dvi_document_get_n_pages;
263  ev_document_class->get_page_size = dvi_document_get_page_size;
264  ev_document_class->render = dvi_document_render;
265  ev_document_class->support_synctex = dvi_document_support_synctex;
266 }
267 
268 /* EvFileExporterIface */
269 static void
272 {
273  DviDocument *dvi_document = DVI_DOCUMENT(exporter);
274 
275  if (dvi_document->exporter_filename)
276  g_free (dvi_document->exporter_filename);
277  dvi_document->exporter_filename = g_strdup (fc->filename);
278 
279  if (dvi_document->exporter_opts) {
280  g_string_free (dvi_document->exporter_opts, TRUE);
281  }
282  dvi_document->exporter_opts = g_string_new ("-s ");
283 }
284 
285 static void
287  EvRenderContext *rc)
288 {
289  DviDocument *dvi_document = DVI_DOCUMENT(exporter);
290 
291  g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
292 }
293 
294 static void
296 {
297  gchar *command_line;
298  gint exit_stat;
299  GError *err = NULL;
300  gboolean success;
301 
302  DviDocument *dvi_document = DVI_DOCUMENT(exporter);
303 
304  command_line = g_strdup_printf ("dvipdfm %s -o %s \"%s\"", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
305  dvi_document->exporter_opts->str,
306  dvi_document->exporter_filename,
307  dvi_document->context->filename);
308 
309  success = g_spawn_command_line_sync (command_line,
310  NULL,
311  NULL,
312  &exit_stat,
313  &err);
314 
315  g_free (command_line);
316 
317  if (success == FALSE) {
318  g_warning ("Error: %s", err->message);
319  } else if (!WIFEXITED(exit_stat) || WEXITSTATUS(exit_stat) != EXIT_SUCCESS){
320  g_warning ("Error: dvipdfm does not end normally or exit with a failure status.");
321  }
322 
323  if (err)
324  g_error_free (err);
325 }
326 
329 {
335 }
336 
337 static void
339 {
344 }
345 
346 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
347 
348 static gboolean
349 hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
350 {
351  float f, p, q, t, r, g, b;
352  int i;
353 
354  s /= 100;
355  v /= 100;
356  h /= 60;
357  i = floor (h);
358  if (i == 6)
359  i = 0;
360  else if ((i > 6) || (i < 0))
361  return FALSE;
362  f = h - i;
363  p = v * (1 - s);
364  q = v * (1 - (s * f));
365  t = v * (1 - (s * (1 - f)));
366 
367  if (i == 0) {
368  r = v;
369  g = t;
370  b = p;
371  } else if (i == 1) {
372  r = q;
373  g = v;
374  b = p;
375  } else if (i == 2) {
376  r = p;
377  g = v;
378  b = t;
379  } else if (i == 3) {
380  r = p;
381  g = q;
382  b = v;
383  } else if (i == 4) {
384  r = t;
385  g = p;
386  b = v;
387  } else if (i == 5) {
388  r = v;
389  g = p;
390  b = q;
391  }
392 
393  *red = (guchar)floor(r * 255.0);
394  *green = (guchar)floor(g * 255.0);
395  *blue = (guchar)floor(b * 255.0);
396 
397  return TRUE;
398 }
399 
400 static void
401 parse_color (const gchar *ptr,
402  gdouble *color,
403  gint n_color)
404 {
405  gchar *p = (gchar *)ptr;
406  gint i;
407 
408  for (i = 0; i < n_color; i++) {
409  while (isspace (*p)) p++;
410  color[i] = g_ascii_strtod (p, NULL);
411  while (!isspace (*p) && *p != '\0') p++;
412  if (*p == '\0')
413  break;
414  }
415 }
416 
417 static void
418 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
419 {
420  if (strncmp (arg, "pop", 3) == 0) {
421  mdvi_pop_color (dvi);
422  } else if (strncmp (arg, "push", 4) == 0) {
423  /* Find color source: Named, CMYK or RGB */
424  const char *tmp = arg + 4;
425 
426  while (isspace (*tmp)) tmp++;
427 
428  if (!strncmp ("rgb", tmp, 3)) {
429  gdouble rgb[3];
430  guchar red, green, blue;
431 
432  parse_color (tmp + 4, rgb, 3);
433 
434  red = 255 * rgb[0];
435  green = 255 * rgb[1];
436  blue = 255 * rgb[2];
437 
438  mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
439  } else if (!strncmp ("hsb", tmp, 4)) {
440  gdouble hsb[3];
441  guchar red, green, blue;
442 
443  parse_color (tmp + 4, hsb, 3);
444 
445  if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
446  mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
447  } else if (!strncmp ("cmyk", tmp, 4)) {
448  gdouble cmyk[4];
449  double r, g, b;
450  guchar red, green, blue;
451 
452  parse_color (tmp + 5, cmyk, 4);
453 
454  r = 1.0 - cmyk[0] - cmyk[3];
455  if (r < 0.0)
456  r = 0.0;
457  g = 1.0 - cmyk[1] - cmyk[3];
458  if (g < 0.0)
459  g = 0.0;
460  b = 1.0 - cmyk[2] - cmyk[3];
461  if (b < 0.0)
462  b = 0.0;
463 
464  red = r * 255 + 0.5;
465  green = g * 255 + 0.5;
466  blue = b * 255 + 0.5;
467 
468  mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
469  } else if (!strncmp ("gray ", tmp, 5)) {
470  gdouble gray;
471  guchar rgb;
472 
473  parse_color (tmp + 5, &gray, 1);
474 
475  rgb = gray * 255 + 0.5;
476 
477  mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
478  } else {
479  GdkColor color;
480 
481  if (gdk_color_parse (tmp, &color)) {
482  guchar red, green, blue;
483 
484  red = color.red * 255 / 65535.;
485  green = color.green * 255 / 65535.;
486  blue = color.blue * 255 / 65535.;
487 
488  mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
489  }
490  }
491  }
492 }
493 
494 static void
496 {
497  dvi_document->params = g_new0 (DviParams, 1);
498 
499  dvi_document->params->dpi = MDVI_DPI;
500  dvi_document->params->vdpi = MDVI_VDPI;
501  dvi_document->params->mag = MDVI_MAGNIFICATION;
502  dvi_document->params->density = MDVI_DEFAULT_DENSITY;
503  dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
504  dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
505  dvi_document->params->hdrift = 0;
506  dvi_document->params->vdrift = 0;
507  dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
508  dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
509  dvi_document->params->orientation = MDVI_ORIENT_TBLR;
510 
511  dvi_document->spec = NULL;
512 
513  dvi_document->params->bg = 0xffffffff;
514  dvi_document->params->fg = 0xff000000;
515 }
516 
517 static void
519 {
520  dvi_document->context = NULL;
521  dvi_document_init_params (dvi_document);
522 
523  dvi_document->exporter_filename = NULL;
524  dvi_document->exporter_opts = NULL;
525 }