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 File Reference
#include "config.h"
#include "dvi-document.h"
#include "texmfcnf.h"
#include "ev-document-misc.h"
#include "ev-file-exporter.h"
#include "ev-file-helpers.h"
#include "mdvi.h"
#include "fonts.h"
#include "color.h"
#include "cairo-device.h"
#include <glib/gi18n-lib.h>
#include <ctype.h>
#include <sys/wait.h>
#include <stdlib.h>
+ Include dependency graph for dvi-document.c:

Go to the source code of this file.

Data Structures

struct  _DviDocumentClass
 
struct  _DviDocument
 

Macros

#define RGB2ULONG(r, g, b)   ((0xFF<<24)|(r<<16)|(g<<8)|(b))
 

Typedefs

typedef struct _DviDocumentClass DviDocumentClass
 

Enumerations

enum  { PROP_0, PROP_TITLE }
 

Functions

static void dvi_document_file_exporter_iface_init (EvFileExporterInterface *iface)
 
static void dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
 
 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,{EV_BACKEND_IMPLEMENT_INTERFACE(EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);})
 
static gboolean dvi_document_load (EvDocument *document, const char *uri, GError **error)
 
static gboolean dvi_document_save (EvDocument *document, const char *uri, GError **error)
 
static int dvi_document_get_n_pages (EvDocument *document)
 
static void dvi_document_get_page_size (EvDocument *document, EvPage *page, double *width, double *height)
 
static cairo_surface_t * dvi_document_render (EvDocument *document, EvRenderContext *rc)
 
static void dvi_document_finalize (GObject *object)
 
static gboolean dvi_document_support_synctex (EvDocument *document)
 
static void dvi_document_class_init (DviDocumentClass *klass)
 
static void dvi_document_file_exporter_begin (EvFileExporter *exporter, EvFileExporterContext *fc)
 
static void dvi_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
 
static void dvi_document_file_exporter_end (EvFileExporter *exporter)
 
static EvFileExporterCapabilities dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
 
static gboolean hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
 
static void parse_color (const gchar *ptr, gdouble *color, gint n_color)
 
static void dvi_document_init_params (DviDocument *dvi_document)
 
static void dvi_document_init (DviDocument *dvi_document)
 

Variables

static GMutex dvi_context_mutex
 

Macro Definition Documentation

#define RGB2ULONG (   r,
  g,
 
)    ((0xFF<<24)|(r<<16)|(g<<8)|(b))

Definition at line 346 of file dvi-document.c.

Typedef Documentation

Definition at line 74 of file dvi-document.c.

Enumeration Type Documentation

anonymous enum
Enumerator
PROP_0 
PROP_TITLE 

Definition at line 45 of file dvi-document.c.

45  {
46  PROP_0,
48 };

Function Documentation

static void dvi_document_class_init ( DviDocumentClass klass)
static

Definition at line 245 of file dvi-document.c.

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 }
static void dvi_document_do_color_special ( DviContext dvi,
const char *  prefix,
const char *  arg 
)
static

Definition at line 418 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static void dvi_document_file_exporter_begin ( EvFileExporter exporter,
EvFileExporterContext fc 
)
static

Definition at line 270 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static void dvi_document_file_exporter_do_page ( EvFileExporter exporter,
EvRenderContext rc 
)
static

Definition at line 286 of file dvi-document.c.

288 {
289  DviDocument *dvi_document = DVI_DOCUMENT(exporter);
290 
291  g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
292 }

+ Here is the caller graph for this function:

static void dvi_document_file_exporter_end ( EvFileExporter exporter)
static

Definition at line 295 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static EvFileExporterCapabilities dvi_document_file_exporter_get_capabilities ( EvFileExporter exporter)
static

Definition at line 328 of file dvi-document.c.

+ Here is the caller graph for this function:

static void dvi_document_file_exporter_iface_init ( EvFileExporterInterface iface)
static
static void dvi_document_finalize ( GObject *  object)
static

Definition at line 213 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static int dvi_document_get_n_pages ( EvDocument document)
static

Definition at line 141 of file dvi-document.c.

142 {
143  DviDocument *dvi_document = DVI_DOCUMENT (document);
144 
145  return dvi_document->context->npages;
146 }

+ Here is the caller graph for this function:

static void dvi_document_get_page_size ( EvDocument document,
EvPage page,
double *  width,
double *  height 
)
static

Definition at line 149 of file dvi-document.c.

153 {
154  DviDocument *dvi_document = DVI_DOCUMENT (document);
155 
156  *width = dvi_document->base_width;
157  *height = dvi_document->base_height;;
158 }

+ Here is the caller graph for this function:

static void dvi_document_init ( DviDocument dvi_document)
static

Definition at line 518 of file dvi-document.c.

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 }
static void dvi_document_init_params ( DviDocument dvi_document)
static

Definition at line 495 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static gboolean dvi_document_load ( EvDocument document,
const char *  uri,
GError **  error 
)
static

Definition at line 87 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static cairo_surface_t* dvi_document_render ( EvDocument document,
EvRenderContext rc 
)
static

Definition at line 161 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static gboolean dvi_document_save ( EvDocument document,
const char *  uri,
GError **  error 
)
static

Definition at line 131 of file dvi-document.c.

134 {
135  DviDocument *dvi_document = DVI_DOCUMENT (document);
136 
137  return ev_xfer_uri_simple (dvi_document->uri, uri, error);
138 }

+ Here is the caller graph for this function:

static gboolean dvi_document_support_synctex ( EvDocument document)
static

Definition at line 239 of file dvi-document.c.

240 {
241  return TRUE;
242 }

+ Here is the caller graph for this function:

EV_BACKEND_REGISTER_WITH_CODE ( DviDocument  ,
dvi_document  ,
{EV_BACKEND_IMPLEMENT_INTERFACE(EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);}   
)
static gboolean hsb2rgb ( float  h,
float  s,
float  v,
guchar *  red,
guchar *  green,
guchar *  blue 
)
static

Definition at line 349 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

static void parse_color ( const gchar *  ptr,
gdouble *  color,
gint  n_color 
)
static

Definition at line 401 of file dvi-document.c.

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 }

+ Here is the caller graph for this function:

Variable Documentation

GMutex dvi_context_mutex
static

Definition at line 43 of file dvi-document.c.