1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
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)
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.
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.
22 #include "dvi-document.h"
24 #include "ev-document-thumbnails.h"
25 #include "ev-document-misc.h"
26 #include "ev-file-exporter.h"
27 #include "ev-file-helpers.h"
32 #include "cairo-device.h"
34 #include <glib/gi18n-lib.h>
37 # define WIFEXITED(x) ((x) != 3)
38 # define WEXITSTATUS(x) (x)
40 # include <sys/wait.h>
44 GMutex *dvi_context_mutex = NULL;
51 struct _DviDocumentClass
53 EvDocumentClass parent_class;
58 EvDocument parent_instance;
64 /* To let document scale we should remember width and height */
71 gchar *exporter_filename;
72 GString *exporter_opts;
75 typedef struct _DviDocumentClass DviDocumentClass;
77 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface);
78 static void dvi_document_file_exporter_iface_init (EvFileExporterInterface *iface);
79 static void dvi_document_do_color_special (DviContext *dvi,
83 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
85 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
86 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
90 dvi_document_load (EvDocument *document,
95 DviDocument *dvi_document = DVI_DOCUMENT(document);
97 filename = g_filename_from_uri (uri, NULL, error);
101 g_mutex_lock (dvi_context_mutex);
102 if (dvi_document->context)
103 mdvi_destroy_context (dvi_document->context);
105 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
106 g_mutex_unlock (dvi_context_mutex);
109 if (!dvi_document->context) {
110 g_set_error_literal (error,
112 EV_DOCUMENT_ERROR_INVALID,
113 _("DVI document has incorrect format"));
117 mdvi_cairo_device_init (&dvi_document->context->device);
120 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
121 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
123 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
124 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
126 g_free (dvi_document->uri);
127 dvi_document->uri = g_strdup (uri);
134 dvi_document_save (EvDocument *document,
138 DviDocument *dvi_document = DVI_DOCUMENT (document);
140 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
144 dvi_document_get_n_pages (EvDocument *document)
146 DviDocument *dvi_document = DVI_DOCUMENT (document);
148 return dvi_document->context->npages;
152 dvi_document_get_page_size (EvDocument *document,
157 DviDocument *dvi_document = DVI_DOCUMENT (document);
159 *width = dvi_document->base_width;
160 *height = dvi_document->base_height;;
163 static cairo_surface_t *
164 dvi_document_render (EvDocument *document,
167 cairo_surface_t *surface;
168 cairo_surface_t *rotated_surface;
169 DviDocument *dvi_document = DVI_DOCUMENT(document);
170 gint required_width, required_height;
171 gint proposed_width, proposed_height;
172 gint xmargin = 0, ymargin = 0;
174 /* We should protect our context since it's not
175 * thread safe. The work to the future -
176 * let context render page independently
178 g_mutex_lock (dvi_context_mutex);
180 mdvi_setpage (dvi_document->context, rc->page->index);
182 mdvi_set_shrink (dvi_document->context,
183 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
184 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
186 required_width = dvi_document->base_width * rc->scale + 0.5;
187 required_height = dvi_document->base_height * rc->scale + 0.5;
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;
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;
196 mdvi_cairo_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
197 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
198 mdvi_cairo_device_render (dvi_document->context);
199 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
201 g_mutex_unlock (dvi_context_mutex);
203 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
207 cairo_surface_destroy (surface);
209 return rotated_surface;
213 dvi_document_finalize (GObject *object)
215 DviDocument *dvi_document = DVI_DOCUMENT(object);
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);
222 g_mutex_unlock (dvi_context_mutex);
224 if (dvi_document->params)
225 g_free (dvi_document->params);
227 if (dvi_document->exporter_filename)
228 g_free (dvi_document->exporter_filename);
230 if (dvi_document->exporter_opts)
231 g_string_free (dvi_document->exporter_opts, TRUE);
233 g_free (dvi_document->uri);
235 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
239 dvi_document_support_synctex (EvDocument *document)
245 dvi_document_class_init (DviDocumentClass *klass)
247 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
248 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
251 gobject_class->finalize = dvi_document_finalize;
253 texmfcnf = get_texmfcnf();
254 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI, texmfcnf);
257 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
258 mdvi_register_fonts ();
260 dvi_context_mutex = g_mutex_new ();
262 ev_document_class->load = dvi_document_load;
263 ev_document_class->save = dvi_document_save;
264 ev_document_class->get_n_pages = dvi_document_get_n_pages;
265 ev_document_class->get_page_size = dvi_document_get_page_size;
266 ev_document_class->render = dvi_document_render;
267 ev_document_class->support_synctex = dvi_document_support_synctex;
271 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
276 DviDocument *dvi_document = DVI_DOCUMENT (document);
277 gdouble page_width = dvi_document->base_width;
278 gdouble page_height = dvi_document->base_height;
280 if (rc->rotation == 90 || rc->rotation == 270) {
281 *width = (gint) (page_height * rc->scale);
282 *height = (gint) (page_width * rc->scale);
284 *width = (gint) (page_width * rc->scale);
285 *height = (gint) (page_height * rc->scale);
290 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
294 DviDocument *dvi_document = DVI_DOCUMENT (document);
296 GdkPixbuf *rotated_pixbuf;
297 cairo_surface_t *surface;
298 gint thumb_width, thumb_height;
299 gint proposed_width, proposed_height;
301 thumb_width = (gint) (dvi_document->base_width * rc->scale);
302 thumb_height = (gint) (dvi_document->base_height * rc->scale);
304 g_mutex_lock (dvi_context_mutex);
306 mdvi_setpage (dvi_document->context, rc->page->index);
308 mdvi_set_shrink (dvi_document->context,
309 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
310 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
312 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
313 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
316 mdvi_cairo_device_set_margins (&dvi_document->context->device,
317 MAX (thumb_width - proposed_width, 0) / 2,
318 MAX (thumb_height - proposed_height, 0) / 2);
320 mdvi_cairo_device_set_margins (&dvi_document->context->device,
321 MAX (thumb_width - proposed_width - 2, 0) / 2,
322 MAX (thumb_height - proposed_height - 2, 0) / 2);
325 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
326 mdvi_cairo_device_render (dvi_document->context);
327 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
328 g_mutex_unlock (dvi_context_mutex);
330 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
331 cairo_surface_destroy (surface);
333 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
334 g_object_unref (pixbuf);
337 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
339 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
340 g_object_unref (tmp_pixbuf);
343 return rotated_pixbuf;
347 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
349 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
350 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
353 /* EvFileExporterIface */
355 dvi_document_file_exporter_begin (EvFileExporter *exporter,
356 EvFileExporterContext *fc)
358 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
360 if (dvi_document->exporter_filename)
361 g_free (dvi_document->exporter_filename);
362 dvi_document->exporter_filename = g_strdup (fc->filename);
364 if (dvi_document->exporter_opts) {
365 g_string_free (dvi_document->exporter_opts, TRUE);
367 dvi_document->exporter_opts = g_string_new ("-s ");
371 dvi_document_file_exporter_do_page (EvFileExporter *exporter,
374 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
376 g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
380 dvi_document_file_exporter_end (EvFileExporter *exporter)
387 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
389 command_line = g_strdup_printf ("dvipdfm %s -o %s \"%s\"", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
390 dvi_document->exporter_opts->str,
391 dvi_document->exporter_filename,
392 dvi_document->context->filename);
394 success = g_spawn_command_line_sync (command_line,
400 g_free (command_line);
402 if (success == FALSE) {
403 g_warning ("Error: %s", err->message);
404 } else if (!WIFEXITED(exit_stat) || WEXITSTATUS(exit_stat) != EXIT_SUCCESS){
405 g_warning ("Error: dvipdfm does not end normally or exit with a failure status.");
412 static EvFileExporterCapabilities
413 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
415 return EV_FILE_EXPORTER_CAN_PAGE_SET |
416 EV_FILE_EXPORTER_CAN_COPIES |
417 EV_FILE_EXPORTER_CAN_COLLATE |
418 EV_FILE_EXPORTER_CAN_REVERSE |
419 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
423 dvi_document_file_exporter_iface_init (EvFileExporterInterface *iface)
425 iface->begin = dvi_document_file_exporter_begin;
426 iface->do_page = dvi_document_file_exporter_do_page;
427 iface->end = dvi_document_file_exporter_end;
428 iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
431 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
434 hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
436 float i, f, p, q, t, r, g, b;
440 else if ((h > 360) || (h < 0))
449 q = v * (1 - (s * f));
450 t = v * (1 - (s * (1 - f)));
478 *red = (guchar)floor(r * 255.0);
479 *green = (guchar)floor(g * 255.0);
480 *blue = (guchar)floor(b * 255.0);
486 parse_color (const gchar *ptr,
490 gchar *p = (gchar *)ptr;
493 for (i = 0; i < n_color; i++) {
494 while (isspace (*p)) p++;
495 color[i] = g_ascii_strtod (p, NULL);
496 while (!isspace (*p) && *p != '\0') p++;
503 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
505 if (strncmp (arg, "pop", 3) == 0) {
506 mdvi_pop_color (dvi);
507 } else if (strncmp (arg, "push", 4) == 0) {
508 /* Find color source: Named, CMYK or RGB */
509 const char *tmp = arg + 4;
511 while (isspace (*tmp)) tmp++;
513 if (!strncmp ("rgb", tmp, 3)) {
515 guchar red, green, blue;
517 parse_color (tmp + 4, rgb, 3);
520 green = 255 * rgb[1];
523 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
524 } else if (!strncmp ("hsb", tmp, 4)) {
526 guchar red, green, blue;
528 parse_color (tmp + 4, hsb, 3);
530 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
531 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
532 } else if (!strncmp ("cmyk", tmp, 4)) {
535 guchar red, green, blue;
537 parse_color (tmp + 5, cmyk, 4);
539 r = 1.0 - cmyk[0] - cmyk[3];
542 g = 1.0 - cmyk[1] - cmyk[3];
545 b = 1.0 - cmyk[2] - cmyk[3];
550 green = g * 255 + 0.5;
551 blue = b * 255 + 0.5;
553 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
554 } else if (!strncmp ("gray ", tmp, 5)) {
558 parse_color (tmp + 5, &gray, 1);
560 rgb = gray * 255 + 0.5;
562 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
566 if (gdk_color_parse (tmp, &color)) {
567 guchar red, green, blue;
569 red = color.red * 255 / 65535.;
570 green = color.green * 255 / 65535.;
571 blue = color.blue * 255 / 65535.;
573 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
580 dvi_document_init_params (DviDocument *dvi_document)
582 dvi_document->params = g_new0 (DviParams, 1);
584 dvi_document->params->dpi = MDVI_DPI;
585 dvi_document->params->vdpi = MDVI_VDPI;
586 dvi_document->params->mag = MDVI_MAGNIFICATION;
587 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
588 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
589 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
590 dvi_document->params->hdrift = 0;
591 dvi_document->params->vdrift = 0;
592 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
593 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
594 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
596 dvi_document->spec = NULL;
598 dvi_document->params->bg = 0xffffffff;
599 dvi_document->params->fg = 0xff000000;
603 dvi_document_init (DviDocument *dvi_document)
605 dvi_document->context = NULL;
606 dvi_document_init_params (dvi_document);
608 dvi_document->exporter_filename = NULL;
609 dvi_document->exporter_opts = NULL;