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 {
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
dvi_document_load(EvDocument * document,const char * uri,GError ** error)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
dvi_document_save(EvDocument * document,const char * uri,GError ** error)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
dvi_document_get_n_pages(EvDocument * document)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
dvi_document_get_page_size(EvDocument * document,EvPage * page,double * width,double * height)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 *
dvi_document_render(EvDocument * document,EvRenderContext * rc)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 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
dvi_document_finalize(GObject * object)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
dvi_document_support_synctex(EvDocument * document)239 dvi_document_support_synctex (EvDocument *document)
240 {
241 return TRUE;
242 }
243
244 static void
dvi_document_class_init(DviDocumentClass * klass)245 dvi_document_class_init (DviDocumentClass *klass)
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();
254 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI, texmfcnf);
255 g_free(texmfcnf);
256
257 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
258 mdvi_register_fonts ();
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
dvi_document_file_exporter_begin(EvFileExporter * exporter,EvFileExporterContext * fc)270 dvi_document_file_exporter_begin (EvFileExporter *exporter,
271 EvFileExporterContext *fc)
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
dvi_document_file_exporter_do_page(EvFileExporter * exporter,EvRenderContext * rc)286 dvi_document_file_exporter_do_page (EvFileExporter *exporter,
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
dvi_document_file_exporter_end(EvFileExporter * exporter)295 dvi_document_file_exporter_end (EvFileExporter *exporter)
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 gchar* quoted_filename = g_shell_quote (dvi_document->context->filename);
304
305 command_line = g_strdup_printf ("dvipdfm %s -o %s %s", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
306 dvi_document->exporter_opts->str,
307 dvi_document->exporter_filename,
308 quoted_filename);
309 g_free (quoted_filename);
310
311 success = g_spawn_command_line_sync (command_line,
312 NULL,
313 NULL,
314 &exit_stat,
315 &err);
316
317 g_free (command_line);
318
319 if (success == FALSE) {
320 g_warning ("Error: %s", err->message);
321 } else if (!WIFEXITED(exit_stat) || WEXITSTATUS(exit_stat) != EXIT_SUCCESS){
322 g_warning ("Error: dvipdfm does not end normally or exit with a failure status.");
323 }
324
325 if (err)
326 g_error_free (err);
327 }
328
329 static EvFileExporterCapabilities
dvi_document_file_exporter_get_capabilities(EvFileExporter * exporter)330 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
331 {
332 return EV_FILE_EXPORTER_CAN_PAGE_SET |
333 EV_FILE_EXPORTER_CAN_COPIES |
334 EV_FILE_EXPORTER_CAN_COLLATE |
335 EV_FILE_EXPORTER_CAN_REVERSE |
336 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
337 }
338
339 static void
dvi_document_file_exporter_iface_init(EvFileExporterInterface * iface)340 dvi_document_file_exporter_iface_init (EvFileExporterInterface *iface)
341 {
342 iface->begin = dvi_document_file_exporter_begin;
343 iface->do_page = dvi_document_file_exporter_do_page;
344 iface->end = dvi_document_file_exporter_end;
345 iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
346 }
347
348 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
349
350 static gboolean
hsb2rgb(float h,float s,float v,guchar * red,guchar * green,guchar * blue)351 hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
352 {
353 float f, p, q, t, r, g, b;
354 int i;
355
356 s /= 100;
357 v /= 100;
358 h /= 60;
359 i = floor (h);
360 if (i == 6)
361 i = 0;
362 else if ((i > 6) || (i < 0))
363 return FALSE;
364 f = h - i;
365 p = v * (1 - s);
366 q = v * (1 - (s * f));
367 t = v * (1 - (s * (1 - f)));
368 r = g = b = 0;
369
370 if (i == 0) {
371 r = v;
372 g = t;
373 b = p;
374 } else if (i == 1) {
375 r = q;
376 g = v;
377 b = p;
378 } else if (i == 2) {
379 r = p;
380 g = v;
381 b = t;
382 } else if (i == 3) {
383 r = p;
384 g = q;
385 b = v;
386 } else if (i == 4) {
387 r = t;
388 g = p;
389 b = v;
390 } else if (i == 5) {
391 r = v;
392 g = p;
393 b = q;
394 }
395
396 *red = (guchar)floor(r * 255.0);
397 *green = (guchar)floor(g * 255.0);
398 *blue = (guchar)floor(b * 255.0);
399
400 return TRUE;
401 }
402
403 static void
parse_color(const gchar * ptr,gdouble * color,gint n_color)404 parse_color (const gchar *ptr,
405 gdouble *color,
406 gint n_color)
407 {
408 gchar *p = (gchar *)ptr;
409 gint i;
410
411 for (i = 0; i < n_color; i++) {
412 while (isspace (*p)) p++;
413 color[i] = g_ascii_strtod (p, NULL);
414 while (!isspace (*p) && *p != '\0') p++;
415 if (*p == '\0')
416 break;
417 }
418 }
419
420 static void
dvi_document_do_color_special(DviContext * dvi,const char * prefix,const char * arg)421 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
422 {
423 if (strncmp (arg, "pop", 3) == 0) {
424 mdvi_pop_color (dvi);
425 } else if (strncmp (arg, "push", 4) == 0) {
426 /* Find color source: Named, CMYK or RGB */
427 const char *tmp = arg + 4;
428
429 while (isspace (*tmp)) tmp++;
430
431 if (!strncmp ("rgb", tmp, 3)) {
432 gdouble rgb[3];
433 guchar red, green, blue;
434
435 parse_color (tmp + 4, rgb, 3);
436
437 red = 255 * rgb[0];
438 green = 255 * rgb[1];
439 blue = 255 * rgb[2];
440
441 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
442 } else if (!strncmp ("hsb", tmp, 4)) {
443 gdouble hsb[3];
444 guchar red, green, blue;
445
446 parse_color (tmp + 4, hsb, 3);
447
448 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
449 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
450 } else if (!strncmp ("cmyk", tmp, 4)) {
451 gdouble cmyk[4];
452 double r, g, b;
453 guchar red, green, blue;
454
455 parse_color (tmp + 5, cmyk, 4);
456
457 r = 1.0 - cmyk[0] - cmyk[3];
458 if (r < 0.0)
459 r = 0.0;
460 g = 1.0 - cmyk[1] - cmyk[3];
461 if (g < 0.0)
462 g = 0.0;
463 b = 1.0 - cmyk[2] - cmyk[3];
464 if (b < 0.0)
465 b = 0.0;
466
467 red = r * 255 + 0.5;
468 green = g * 255 + 0.5;
469 blue = b * 255 + 0.5;
470
471 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
472 } else if (!strncmp ("gray ", tmp, 5)) {
473 gdouble gray;
474 guchar rgb;
475
476 parse_color (tmp + 5, &gray, 1);
477
478 rgb = gray * 255 + 0.5;
479
480 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
481 } else {
482 GdkColor color;
483
484 if (gdk_color_parse (tmp, &color)) {
485 guchar red, green, blue;
486
487 red = color.red * 255 / 65535.;
488 green = color.green * 255 / 65535.;
489 blue = color.blue * 255 / 65535.;
490
491 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
492 }
493 }
494 }
495 }
496
497 static void
dvi_document_init_params(DviDocument * dvi_document)498 dvi_document_init_params (DviDocument *dvi_document)
499 {
500 dvi_document->params = g_new0 (DviParams, 1);
501
502 dvi_document->params->dpi = MDVI_DPI;
503 dvi_document->params->vdpi = MDVI_VDPI;
504 dvi_document->params->mag = MDVI_MAGNIFICATION;
505 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
506 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
507 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
508 dvi_document->params->hdrift = 0;
509 dvi_document->params->vdrift = 0;
510 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
511 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
512 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
513
514 dvi_document->spec = NULL;
515
516 dvi_document->params->bg = 0xffffffff;
517 dvi_document->params->fg = 0xff000000;
518 }
519
520 static void
dvi_document_init(DviDocument * dvi_document)521 dvi_document_init (DviDocument *dvi_document)
522 {
523 dvi_document->context = NULL;
524 dvi_document_init_params (dvi_document);
525
526 dvi_document->exporter_filename = NULL;
527 dvi_document->exporter_opts = NULL;
528 }
529