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"
29 #include "cairo-device.h"
31 #include <glib/gi18n.h>
33 GMutex *dvi_context_mutex = NULL;
40 struct _DviDocumentClass
42 GObjectClass parent_class;
47 GObject parent_instance;
53 /* To let document scale we should remember width and height */
60 gchar *exporter_filename;
61 GString *exporter_opts;
64 typedef struct _DviDocumentClass DviDocumentClass;
66 static void dvi_document_document_iface_init (EvDocumentIface *iface);
67 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
68 static void dvi_document_file_exporter_iface_init (EvFileExporterIface *iface);
69 static void dvi_document_get_page_size (EvDocument *document,
73 static void dvi_document_do_color_special (DviContext *dvi,
77 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
79 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
80 G_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);
96 EV_DOCUMENT_ERROR_INVALID,
97 _("File not available"));
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);
108 if (!dvi_document->context) {
111 EV_DOCUMENT_ERROR_INVALID,
112 _("DVI document has incorrect format"));
116 mdvi_cairo_device_init (&dvi_document->context->device);
119 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
120 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
122 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
123 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
125 g_free (dvi_document->uri);
126 dvi_document->uri = g_strdup (uri);
133 dvi_document_save (EvDocument *document,
137 DviDocument *dvi_document = DVI_DOCUMENT (document);
139 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
143 dvi_document_get_n_pages (EvDocument *document)
145 DviDocument *dvi_document = DVI_DOCUMENT (document);
147 return dvi_document->context->npages;
151 dvi_document_get_page_size (EvDocument *document,
156 DviDocument *dvi_document = DVI_DOCUMENT (document);
158 *width = dvi_document->base_width;
159 *height = dvi_document->base_height;;
162 static cairo_surface_t *
163 dvi_document_render (EvDocument *document,
166 cairo_surface_t *surface;
167 cairo_surface_t *rotated_surface;
168 DviDocument *dvi_document = DVI_DOCUMENT(document);
169 gint required_width, required_height;
170 gint proposed_width, proposed_height;
171 gint xmargin = 0, ymargin = 0;
173 /* We should protect our context since it's not
174 * thread safe. The work to the future -
175 * let context render page independently
177 g_mutex_lock (dvi_context_mutex);
179 mdvi_setpage (dvi_document->context, rc->page);
181 mdvi_set_shrink (dvi_document->context,
182 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
183 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
185 required_width = dvi_document->base_width * rc->scale + 0.5;
186 required_height = dvi_document->base_height * rc->scale + 0.5;
187 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
188 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
190 if (required_width >= proposed_width)
191 xmargin = (required_width - proposed_width) / 2;
192 if (required_height >= proposed_height)
193 ymargin = (required_height - proposed_height) / 2;
195 mdvi_cairo_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
196 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
197 mdvi_cairo_device_render (dvi_document->context);
198 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
200 g_mutex_unlock (dvi_context_mutex);
202 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
206 cairo_surface_destroy (surface);
208 return rotated_surface;
212 dvi_document_finalize (GObject *object)
214 DviDocument *dvi_document = DVI_DOCUMENT(object);
216 g_mutex_lock (dvi_context_mutex);
217 if (dvi_document->context) {
218 mdvi_cairo_device_free (&dvi_document->context->device);
219 mdvi_destroy_context (dvi_document->context);
221 g_mutex_unlock (dvi_context_mutex);
223 if (dvi_document->params)
224 g_free (dvi_document->params);
226 if (dvi_document->exporter_filename)
227 g_free (dvi_document->exporter_filename);
229 if (dvi_document->exporter_opts)
230 g_string_free (dvi_document->exporter_opts, TRUE);
232 g_free (dvi_document->uri);
234 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
239 dvi_document_class_init (DviDocumentClass *klass)
241 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
243 gobject_class->finalize = dvi_document_finalize;
245 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
246 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
247 mdvi_register_fonts ();
249 dvi_context_mutex = g_mutex_new ();
252 static EvDocumentInfo *
253 dvi_document_get_info (EvDocument *document)
255 EvDocumentInfo *info;
257 info = g_new0 (EvDocumentInfo, 1);
263 dvi_document_document_iface_init (EvDocumentIface *iface)
265 iface->load = dvi_document_load;
266 iface->save = dvi_document_save;
267 iface->get_n_pages = dvi_document_get_n_pages;
268 iface->get_page_size = dvi_document_get_page_size;
269 iface->render = dvi_document_render;
270 iface->get_info = dvi_document_get_info;
274 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
279 DviDocument *dvi_document = DVI_DOCUMENT (document);
280 gdouble page_width = dvi_document->base_width;
281 gdouble page_height = dvi_document->base_height;
283 if (rc->rotation == 90 || rc->rotation == 270) {
284 *width = (gint) (page_height * rc->scale);
285 *height = (gint) (page_width * rc->scale);
287 *width = (gint) (page_width * rc->scale);
288 *height = (gint) (page_height * rc->scale);
293 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
297 DviDocument *dvi_document = DVI_DOCUMENT (document);
299 GdkPixbuf *rotated_pixbuf;
300 cairo_surface_t *surface;
301 gint thumb_width, thumb_height;
302 gint proposed_width, proposed_height;
304 thumb_width = (gint) (dvi_document->base_width * rc->scale);
305 thumb_height = (gint) (dvi_document->base_height * rc->scale);
307 g_mutex_lock (dvi_context_mutex);
309 mdvi_setpage (dvi_document->context, rc->page);
311 mdvi_set_shrink (dvi_document->context,
312 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
313 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
315 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
316 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
319 mdvi_cairo_device_set_margins (&dvi_document->context->device,
320 MAX (thumb_width - proposed_width, 0) / 2,
321 MAX (thumb_height - proposed_height, 0) / 2);
323 mdvi_cairo_device_set_margins (&dvi_document->context->device,
324 MAX (thumb_width - proposed_width - 2, 0) / 2,
325 MAX (thumb_height - proposed_height - 2, 0) / 2);
328 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
329 mdvi_cairo_device_render (dvi_document->context);
330 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
331 g_mutex_unlock (dvi_context_mutex);
333 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
334 cairo_surface_destroy (surface);
336 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
337 g_object_unref (pixbuf);
340 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
342 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
343 g_object_unref (tmp_pixbuf);
346 return rotated_pixbuf;
350 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
352 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
353 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
356 /* EvFileExporterIface */
358 dvi_document_file_exporter_begin (EvFileExporter *exporter,
359 EvFileExporterContext *fc)
361 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
363 if (dvi_document->exporter_filename)
364 g_free (dvi_document->exporter_filename);
365 dvi_document->exporter_filename = g_strdup(fc->filename);
367 if (dvi_document->exporter_opts) {
368 g_string_free (dvi_document->exporter_opts, TRUE);
370 dvi_document->exporter_opts = g_string_new ("-s ");
374 dvi_document_file_exporter_do_page (EvFileExporter *exporter,
377 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
379 g_string_append_printf(dvi_document->exporter_opts, "%d,", (rc->page)+1);
383 dvi_document_file_exporter_end (EvFileExporter *exporter)
390 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
392 command_line = g_strdup_printf ("dvipdfm %s -o %s %s", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
393 dvi_document->exporter_opts->str,
394 dvi_document->exporter_filename,
395 dvi_document->context->filename);
397 success = g_spawn_command_line_sync (command_line,
403 g_free(command_line);
405 if (success == FALSE) {
406 g_warning ("Error: %s", err->message);
407 } else if (exit_stat != 0) {
408 g_warning ("Error: dvipdfm exited with non-zero status.");
415 static EvFileExporterCapabilities
416 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
418 return EV_FILE_EXPORTER_CAN_PAGE_SET |
419 EV_FILE_EXPORTER_CAN_COPIES |
420 EV_FILE_EXPORTER_CAN_COLLATE |
421 EV_FILE_EXPORTER_CAN_REVERSE |
422 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
426 dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
428 iface->begin = dvi_document_file_exporter_begin;
429 iface->do_page = dvi_document_file_exporter_do_page;
430 iface->end = dvi_document_file_exporter_end;
431 iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
434 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
437 hsb2rgb (float h, float s, float v, char *red, char *green, char *blue)
439 float i, f, p, q, t, r, g, b;
443 else if ((h > 360) || (h < 0))
452 q = v * (1 - (s * f));
453 t = v * (1 - (s * (1 - f)));
481 (*red) = (char)floor(r * 255);
482 (*green) = (char)floor(g * 255);
483 (*blue) = (char)floor(b * 255);
489 parse_color (const gchar *ptr,
493 gchar *p = (gchar *)ptr;
496 for (i = 0; i < n_color; i++) {
497 while (isspace (*p)) p++;
498 color[i] = g_ascii_strtod (p, NULL);
499 while (!isspace (*p) && *p != '\0') p++;
506 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
508 if (strncmp (arg, "pop", 3) == 0) {
509 mdvi_pop_color (dvi);
510 } else if (strncmp (arg, "push", 4) == 0) {
511 /* Find color source: Named, CMYK or RGB */
512 const char *tmp = arg + 4;
514 while (isspace (*tmp)) tmp++;
516 if (!strncmp ("rgb", tmp, 3)) {
518 guchar red, green, blue;
520 parse_color (tmp + 4, rgb, 3);
523 green = 255 * rgb[1];
526 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
527 } else if (!strncmp ("hsb", tmp, 4)) {
529 guchar red, green, blue;
531 parse_color (tmp + 4, hsb, 3);
533 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
534 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
535 } else if (!strncmp ("cmyk", tmp, 4)) {
538 guchar red, green, blue;
540 parse_color (tmp + 5, cmyk, 4);
542 r = 1.0 - cmyk[0] - cmyk[3];
545 g = 1.0 - cmyk[1] - cmyk[3];
548 b = 1.0 - cmyk[2] - cmyk[3];
553 green = g * 255 + 0.5;
554 blue = b * 255 + 0.5;
556 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
557 } else if (!strncmp ("gray ", tmp, 5)) {
561 parse_color (tmp + 5, &gray, 1);
563 rgb = gray * 255 + 0.5;
565 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
569 if (gdk_color_parse (tmp, &color)) {
570 guchar red, green, blue;
572 red = color.red * 255 / 65535.;
573 green = color.green * 255 / 65535.;
574 blue = color.blue * 255 / 65535.;
576 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
583 dvi_document_init_params (DviDocument *dvi_document)
585 dvi_document->params = g_new0 (DviParams, 1);
587 dvi_document->params->dpi = MDVI_DPI;
588 dvi_document->params->vdpi = MDVI_VDPI;
589 dvi_document->params->mag = MDVI_MAGNIFICATION;
590 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
591 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
592 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
593 dvi_document->params->hdrift = 0;
594 dvi_document->params->vdrift = 0;
595 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
596 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
597 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
599 dvi_document->spec = NULL;
601 dvi_document->params->bg = 0xffffffff;
602 dvi_document->params->fg = 0xff000000;
606 dvi_document_init (DviDocument *dvi_document)
608 dvi_document->context = NULL;
609 dvi_document_init_params (dvi_document);
611 dvi_document->exporter_filename = NULL;
612 dvi_document->exporter_opts = NULL;