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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "dvi-document.h"
23 #include "ev-document-thumbnails.h"
24 #include "ev-document-misc.h"
25 #include "ev-file-exporter.h"
26 #include "ev-file-helpers.h"
31 #include "cairo-device.h"
33 #include <glib/gi18n-lib.h>
38 GMutex *dvi_context_mutex = NULL;
45 struct _DviDocumentClass
47 EvDocumentClass parent_class;
52 EvDocument parent_instance;
58 /* To let document scale we should remember width and height */
65 gchar *exporter_filename;
66 GString *exporter_opts;
69 typedef struct _DviDocumentClass DviDocumentClass;
71 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
72 static void dvi_document_file_exporter_iface_init (EvFileExporterIface *iface);
73 static void dvi_document_do_color_special (DviContext *dvi,
77 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
79 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
80 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
84 dvi_document_load (EvDocument *document,
89 DviDocument *dvi_document = DVI_DOCUMENT(document);
91 filename = g_filename_from_uri (uri, NULL, error);
95 g_mutex_lock (dvi_context_mutex);
96 if (dvi_document->context)
97 mdvi_destroy_context (dvi_document->context);
99 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
100 g_mutex_unlock (dvi_context_mutex);
103 if (!dvi_document->context) {
104 g_set_error_literal (error,
106 EV_DOCUMENT_ERROR_INVALID,
107 _("DVI document has incorrect format"));
111 mdvi_cairo_device_init (&dvi_document->context->device);
114 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
115 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
117 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
118 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
120 g_free (dvi_document->uri);
121 dvi_document->uri = g_strdup (uri);
128 dvi_document_save (EvDocument *document,
132 DviDocument *dvi_document = DVI_DOCUMENT (document);
134 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
138 dvi_document_get_n_pages (EvDocument *document)
140 DviDocument *dvi_document = DVI_DOCUMENT (document);
142 return dvi_document->context->npages;
146 dvi_document_get_page_size (EvDocument *document,
151 DviDocument *dvi_document = DVI_DOCUMENT (document);
153 *width = dvi_document->base_width;
154 *height = dvi_document->base_height;;
157 static cairo_surface_t *
158 dvi_document_render (EvDocument *document,
161 cairo_surface_t *surface;
162 cairo_surface_t *rotated_surface;
163 DviDocument *dvi_document = DVI_DOCUMENT(document);
164 gint required_width, required_height;
165 gint proposed_width, proposed_height;
166 gint xmargin = 0, ymargin = 0;
168 /* We should protect our context since it's not
169 * thread safe. The work to the future -
170 * let context render page independently
172 g_mutex_lock (dvi_context_mutex);
174 mdvi_setpage (dvi_document->context, rc->page->index);
176 mdvi_set_shrink (dvi_document->context,
177 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
178 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
180 required_width = dvi_document->base_width * rc->scale + 0.5;
181 required_height = dvi_document->base_height * rc->scale + 0.5;
182 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
183 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
185 if (required_width >= proposed_width)
186 xmargin = (required_width - proposed_width) / 2;
187 if (required_height >= proposed_height)
188 ymargin = (required_height - proposed_height) / 2;
190 mdvi_cairo_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
191 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
192 mdvi_cairo_device_render (dvi_document->context);
193 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
195 g_mutex_unlock (dvi_context_mutex);
197 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
201 cairo_surface_destroy (surface);
203 return rotated_surface;
207 dvi_document_finalize (GObject *object)
209 DviDocument *dvi_document = DVI_DOCUMENT(object);
211 g_mutex_lock (dvi_context_mutex);
212 if (dvi_document->context) {
213 mdvi_cairo_device_free (&dvi_document->context->device);
214 mdvi_destroy_context (dvi_document->context);
216 g_mutex_unlock (dvi_context_mutex);
218 if (dvi_document->params)
219 g_free (dvi_document->params);
221 if (dvi_document->exporter_filename)
222 g_free (dvi_document->exporter_filename);
224 if (dvi_document->exporter_opts)
225 g_string_free (dvi_document->exporter_opts, TRUE);
227 g_free (dvi_document->uri);
229 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
233 dvi_document_class_init (DviDocumentClass *klass)
235 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
238 gobject_class->finalize = dvi_document_finalize;
240 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
241 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
242 mdvi_register_fonts ();
244 dvi_context_mutex = g_mutex_new ();
246 ev_document_class->load = dvi_document_load;
247 ev_document_class->save = dvi_document_save;
248 ev_document_class->get_n_pages = dvi_document_get_n_pages;
249 ev_document_class->get_page_size = dvi_document_get_page_size;
250 ev_document_class->render = dvi_document_render;
254 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
259 DviDocument *dvi_document = DVI_DOCUMENT (document);
260 gdouble page_width = dvi_document->base_width;
261 gdouble page_height = dvi_document->base_height;
263 if (rc->rotation == 90 || rc->rotation == 270) {
264 *width = (gint) (page_height * rc->scale);
265 *height = (gint) (page_width * rc->scale);
267 *width = (gint) (page_width * rc->scale);
268 *height = (gint) (page_height * rc->scale);
273 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
277 DviDocument *dvi_document = DVI_DOCUMENT (document);
279 GdkPixbuf *rotated_pixbuf;
280 cairo_surface_t *surface;
281 gint thumb_width, thumb_height;
282 gint proposed_width, proposed_height;
284 thumb_width = (gint) (dvi_document->base_width * rc->scale);
285 thumb_height = (gint) (dvi_document->base_height * rc->scale);
287 g_mutex_lock (dvi_context_mutex);
289 mdvi_setpage (dvi_document->context, rc->page->index);
291 mdvi_set_shrink (dvi_document->context,
292 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
293 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
295 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
296 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
299 mdvi_cairo_device_set_margins (&dvi_document->context->device,
300 MAX (thumb_width - proposed_width, 0) / 2,
301 MAX (thumb_height - proposed_height, 0) / 2);
303 mdvi_cairo_device_set_margins (&dvi_document->context->device,
304 MAX (thumb_width - proposed_width - 2, 0) / 2,
305 MAX (thumb_height - proposed_height - 2, 0) / 2);
308 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
309 mdvi_cairo_device_render (dvi_document->context);
310 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
311 g_mutex_unlock (dvi_context_mutex);
313 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
314 cairo_surface_destroy (surface);
316 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
317 g_object_unref (pixbuf);
320 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
322 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
323 g_object_unref (tmp_pixbuf);
326 return rotated_pixbuf;
330 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
332 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
333 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
336 /* EvFileExporterIface */
338 dvi_document_file_exporter_begin (EvFileExporter *exporter,
339 EvFileExporterContext *fc)
341 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
343 if (dvi_document->exporter_filename)
344 g_free (dvi_document->exporter_filename);
345 dvi_document->exporter_filename = g_strdup (fc->filename);
347 if (dvi_document->exporter_opts) {
348 g_string_free (dvi_document->exporter_opts, TRUE);
350 dvi_document->exporter_opts = g_string_new ("-s ");
354 dvi_document_file_exporter_do_page (EvFileExporter *exporter,
357 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
359 g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
363 dvi_document_file_exporter_end (EvFileExporter *exporter)
370 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
372 command_line = g_strdup_printf ("dvipdfm %s -o %s \"%s\"", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
373 dvi_document->exporter_opts->str,
374 dvi_document->exporter_filename,
375 dvi_document->context->filename);
377 success = g_spawn_command_line_sync (command_line,
383 g_free (command_line);
385 if (success == FALSE) {
386 g_warning ("Error: %s", err->message);
387 } else if (!WIFEXITED(exit_stat) || WEXITSTATUS(exit_stat) != EXIT_SUCCESS){
388 g_warning ("Error: dvipdfm does not end normally or exit with a failure status.");
395 static EvFileExporterCapabilities
396 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
398 return EV_FILE_EXPORTER_CAN_PAGE_SET |
399 EV_FILE_EXPORTER_CAN_COPIES |
400 EV_FILE_EXPORTER_CAN_COLLATE |
401 EV_FILE_EXPORTER_CAN_REVERSE |
402 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
406 dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
408 iface->begin = dvi_document_file_exporter_begin;
409 iface->do_page = dvi_document_file_exporter_do_page;
410 iface->end = dvi_document_file_exporter_end;
411 iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
414 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
417 hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
419 float i, f, p, q, t, r, g, b;
423 else if ((h > 360) || (h < 0))
432 q = v * (1 - (s * f));
433 t = v * (1 - (s * (1 - f)));
461 *red = (guchar)floor(r * 255.0);
462 *green = (guchar)floor(g * 255.0);
463 *blue = (guchar)floor(b * 255.0);
469 parse_color (const gchar *ptr,
473 gchar *p = (gchar *)ptr;
476 for (i = 0; i < n_color; i++) {
477 while (isspace (*p)) p++;
478 color[i] = g_ascii_strtod (p, NULL);
479 while (!isspace (*p) && *p != '\0') p++;
486 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
488 if (strncmp (arg, "pop", 3) == 0) {
489 mdvi_pop_color (dvi);
490 } else if (strncmp (arg, "push", 4) == 0) {
491 /* Find color source: Named, CMYK or RGB */
492 const char *tmp = arg + 4;
494 while (isspace (*tmp)) tmp++;
496 if (!strncmp ("rgb", tmp, 3)) {
498 guchar red, green, blue;
500 parse_color (tmp + 4, rgb, 3);
503 green = 255 * rgb[1];
506 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
507 } else if (!strncmp ("hsb", tmp, 4)) {
509 guchar red, green, blue;
511 parse_color (tmp + 4, hsb, 3);
513 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
514 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
515 } else if (!strncmp ("cmyk", tmp, 4)) {
518 guchar red, green, blue;
520 parse_color (tmp + 5, cmyk, 4);
522 r = 1.0 - cmyk[0] - cmyk[3];
525 g = 1.0 - cmyk[1] - cmyk[3];
528 b = 1.0 - cmyk[2] - cmyk[3];
533 green = g * 255 + 0.5;
534 blue = b * 255 + 0.5;
536 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
537 } else if (!strncmp ("gray ", tmp, 5)) {
541 parse_color (tmp + 5, &gray, 1);
543 rgb = gray * 255 + 0.5;
545 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
549 if (gdk_color_parse (tmp, &color)) {
550 guchar red, green, blue;
552 red = color.red * 255 / 65535.;
553 green = color.green * 255 / 65535.;
554 blue = color.blue * 255 / 65535.;
556 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
563 dvi_document_init_params (DviDocument *dvi_document)
565 dvi_document->params = g_new0 (DviParams, 1);
567 dvi_document->params->dpi = MDVI_DPI;
568 dvi_document->params->vdpi = MDVI_VDPI;
569 dvi_document->params->mag = MDVI_MAGNIFICATION;
570 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
571 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
572 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
573 dvi_document->params->hdrift = 0;
574 dvi_document->params->vdrift = 0;
575 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
576 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
577 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
579 dvi_document->spec = NULL;
581 dvi_document->params->bg = 0xffffffff;
582 dvi_document->params->fg = 0xff000000;
586 dvi_document_init (DviDocument *dvi_document)
588 dvi_document->context = NULL;
589 dvi_document_init_params (dvi_document);
591 dvi_document->exporter_filename = NULL;
592 dvi_document->exporter_opts = NULL;