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>
36 GMutex *dvi_context_mutex = NULL;
43 struct _DviDocumentClass
45 GObjectClass parent_class;
50 GObject parent_instance;
56 /* To let document scale we should remember width and height */
63 gchar *exporter_filename;
64 GString *exporter_opts;
67 typedef struct _DviDocumentClass DviDocumentClass;
69 static void dvi_document_document_iface_init (EvDocumentIface *iface);
70 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
71 static void dvi_document_file_exporter_iface_init (EvFileExporterIface *iface);
72 static void dvi_document_do_color_special (DviContext *dvi,
76 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
78 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
79 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
83 dvi_document_load (EvDocument *document,
88 DviDocument *dvi_document = DVI_DOCUMENT(document);
90 filename = g_filename_from_uri (uri, NULL, error);
94 g_mutex_lock (dvi_context_mutex);
95 if (dvi_document->context)
96 mdvi_destroy_context (dvi_document->context);
98 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
99 g_mutex_unlock (dvi_context_mutex);
102 if (!dvi_document->context) {
103 g_set_error_literal (error,
105 EV_DOCUMENT_ERROR_INVALID,
106 _("DVI document has incorrect format"));
110 mdvi_cairo_device_init (&dvi_document->context->device);
113 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
114 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
116 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
117 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
119 g_free (dvi_document->uri);
120 dvi_document->uri = g_strdup (uri);
127 dvi_document_save (EvDocument *document,
131 DviDocument *dvi_document = DVI_DOCUMENT (document);
133 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
137 dvi_document_get_n_pages (EvDocument *document)
139 DviDocument *dvi_document = DVI_DOCUMENT (document);
141 return dvi_document->context->npages;
145 dvi_document_get_page_size (EvDocument *document,
150 DviDocument *dvi_document = DVI_DOCUMENT (document);
152 *width = dvi_document->base_width;
153 *height = dvi_document->base_height;;
156 static cairo_surface_t *
157 dvi_document_render (EvDocument *document,
160 cairo_surface_t *surface;
161 cairo_surface_t *rotated_surface;
162 DviDocument *dvi_document = DVI_DOCUMENT(document);
163 gint required_width, required_height;
164 gint proposed_width, proposed_height;
165 gint xmargin = 0, ymargin = 0;
167 /* We should protect our context since it's not
168 * thread safe. The work to the future -
169 * let context render page independently
171 g_mutex_lock (dvi_context_mutex);
173 mdvi_setpage (dvi_document->context, rc->page->index);
175 mdvi_set_shrink (dvi_document->context,
176 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
177 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
179 required_width = dvi_document->base_width * rc->scale + 0.5;
180 required_height = dvi_document->base_height * rc->scale + 0.5;
181 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
182 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
184 if (required_width >= proposed_width)
185 xmargin = (required_width - proposed_width) / 2;
186 if (required_height >= proposed_height)
187 ymargin = (required_height - proposed_height) / 2;
189 mdvi_cairo_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
190 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
191 mdvi_cairo_device_render (dvi_document->context);
192 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
194 g_mutex_unlock (dvi_context_mutex);
196 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
200 cairo_surface_destroy (surface);
202 return rotated_surface;
206 dvi_document_finalize (GObject *object)
208 DviDocument *dvi_document = DVI_DOCUMENT(object);
210 g_mutex_lock (dvi_context_mutex);
211 if (dvi_document->context) {
212 mdvi_cairo_device_free (&dvi_document->context->device);
213 mdvi_destroy_context (dvi_document->context);
215 g_mutex_unlock (dvi_context_mutex);
217 if (dvi_document->params)
218 g_free (dvi_document->params);
220 if (dvi_document->exporter_filename)
221 g_free (dvi_document->exporter_filename);
223 if (dvi_document->exporter_opts)
224 g_string_free (dvi_document->exporter_opts, TRUE);
226 g_free (dvi_document->uri);
228 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
233 dvi_document_class_init (DviDocumentClass *klass)
235 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
237 gobject_class->finalize = dvi_document_finalize;
239 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
240 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
241 mdvi_register_fonts ();
243 dvi_context_mutex = g_mutex_new ();
246 static EvDocumentInfo *
247 dvi_document_get_info (EvDocument *document)
249 EvDocumentInfo *info;
251 info = g_new0 (EvDocumentInfo, 1);
257 dvi_document_document_iface_init (EvDocumentIface *iface)
259 iface->load = dvi_document_load;
260 iface->save = dvi_document_save;
261 iface->get_n_pages = dvi_document_get_n_pages;
262 iface->get_page_size = dvi_document_get_page_size;
263 iface->render = dvi_document_render;
264 iface->get_info = dvi_document_get_info;
268 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
273 DviDocument *dvi_document = DVI_DOCUMENT (document);
274 gdouble page_width = dvi_document->base_width;
275 gdouble page_height = dvi_document->base_height;
277 if (rc->rotation == 90 || rc->rotation == 270) {
278 *width = (gint) (page_height * rc->scale);
279 *height = (gint) (page_width * rc->scale);
281 *width = (gint) (page_width * rc->scale);
282 *height = (gint) (page_height * rc->scale);
287 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
291 DviDocument *dvi_document = DVI_DOCUMENT (document);
293 GdkPixbuf *rotated_pixbuf;
294 cairo_surface_t *surface;
295 gint thumb_width, thumb_height;
296 gint proposed_width, proposed_height;
298 thumb_width = (gint) (dvi_document->base_width * rc->scale);
299 thumb_height = (gint) (dvi_document->base_height * rc->scale);
301 g_mutex_lock (dvi_context_mutex);
303 mdvi_setpage (dvi_document->context, rc->page->index);
305 mdvi_set_shrink (dvi_document->context,
306 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
307 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
309 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
310 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
313 mdvi_cairo_device_set_margins (&dvi_document->context->device,
314 MAX (thumb_width - proposed_width, 0) / 2,
315 MAX (thumb_height - proposed_height, 0) / 2);
317 mdvi_cairo_device_set_margins (&dvi_document->context->device,
318 MAX (thumb_width - proposed_width - 2, 0) / 2,
319 MAX (thumb_height - proposed_height - 2, 0) / 2);
322 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
323 mdvi_cairo_device_render (dvi_document->context);
324 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
325 g_mutex_unlock (dvi_context_mutex);
327 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
328 cairo_surface_destroy (surface);
330 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
331 g_object_unref (pixbuf);
334 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
336 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
337 g_object_unref (tmp_pixbuf);
340 return rotated_pixbuf;
344 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
346 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
347 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
350 /* EvFileExporterIface */
352 dvi_document_file_exporter_begin (EvFileExporter *exporter,
353 EvFileExporterContext *fc)
355 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
357 if (dvi_document->exporter_filename)
358 g_free (dvi_document->exporter_filename);
359 dvi_document->exporter_filename = g_strdup (fc->filename);
361 if (dvi_document->exporter_opts) {
362 g_string_free (dvi_document->exporter_opts, TRUE);
364 dvi_document->exporter_opts = g_string_new ("-s ");
368 dvi_document_file_exporter_do_page (EvFileExporter *exporter,
371 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
373 g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
377 dvi_document_file_exporter_end (EvFileExporter *exporter)
384 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
386 command_line = g_strdup_printf ("dvipdfm %s -o %s \"%s\"", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
387 dvi_document->exporter_opts->str,
388 dvi_document->exporter_filename,
389 dvi_document->context->filename);
391 success = g_spawn_command_line_sync (command_line,
397 g_free (command_line);
399 if (success == FALSE) {
400 g_warning ("Error: %s", err->message);
401 } else if (exit_stat != 0) {
402 g_warning ("Error: dvipdfm exited with non-zero status.");
409 static EvFileExporterCapabilities
410 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
412 return EV_FILE_EXPORTER_CAN_PAGE_SET |
413 EV_FILE_EXPORTER_CAN_COPIES |
414 EV_FILE_EXPORTER_CAN_COLLATE |
415 EV_FILE_EXPORTER_CAN_REVERSE |
416 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
420 dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
422 iface->begin = dvi_document_file_exporter_begin;
423 iface->do_page = dvi_document_file_exporter_do_page;
424 iface->end = dvi_document_file_exporter_end;
425 iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
428 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
431 hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
433 float i, f, p, q, t, r, g, b;
437 else if ((h > 360) || (h < 0))
446 q = v * (1 - (s * f));
447 t = v * (1 - (s * (1 - f)));
475 *red = (guchar)floor(r * 255.0);
476 *green = (guchar)floor(g * 255.0);
477 *blue = (guchar)floor(b * 255.0);
483 parse_color (const gchar *ptr,
487 gchar *p = (gchar *)ptr;
490 for (i = 0; i < n_color; i++) {
491 while (isspace (*p)) p++;
492 color[i] = g_ascii_strtod (p, NULL);
493 while (!isspace (*p) && *p != '\0') p++;
500 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
502 if (strncmp (arg, "pop", 3) == 0) {
503 mdvi_pop_color (dvi);
504 } else if (strncmp (arg, "push", 4) == 0) {
505 /* Find color source: Named, CMYK or RGB */
506 const char *tmp = arg + 4;
508 while (isspace (*tmp)) tmp++;
510 if (!strncmp ("rgb", tmp, 3)) {
512 guchar red, green, blue;
514 parse_color (tmp + 4, rgb, 3);
517 green = 255 * rgb[1];
520 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
521 } else if (!strncmp ("hsb", tmp, 4)) {
523 guchar red, green, blue;
525 parse_color (tmp + 4, hsb, 3);
527 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
528 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
529 } else if (!strncmp ("cmyk", tmp, 4)) {
532 guchar red, green, blue;
534 parse_color (tmp + 5, cmyk, 4);
536 r = 1.0 - cmyk[0] - cmyk[3];
539 g = 1.0 - cmyk[1] - cmyk[3];
542 b = 1.0 - cmyk[2] - cmyk[3];
547 green = g * 255 + 0.5;
548 blue = b * 255 + 0.5;
550 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
551 } else if (!strncmp ("gray ", tmp, 5)) {
555 parse_color (tmp + 5, &gray, 1);
557 rgb = gray * 255 + 0.5;
559 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
563 if (gdk_color_parse (tmp, &color)) {
564 guchar red, green, blue;
566 red = color.red * 255 / 65535.;
567 green = color.green * 255 / 65535.;
568 blue = color.blue * 255 / 65535.;
570 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
577 dvi_document_init_params (DviDocument *dvi_document)
579 dvi_document->params = g_new0 (DviParams, 1);
581 dvi_document->params->dpi = MDVI_DPI;
582 dvi_document->params->vdpi = MDVI_VDPI;
583 dvi_document->params->mag = MDVI_MAGNIFICATION;
584 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
585 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
586 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
587 dvi_document->params->hdrift = 0;
588 dvi_document->params->vdrift = 0;
589 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
590 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
591 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
593 dvi_document->spec = NULL;
595 dvi_document->params->bg = 0xffffffff;
596 dvi_document->params->fg = 0xff000000;
600 dvi_document_init (DviDocument *dvi_document)
602 dvi_document->context = NULL;
603 dvi_document_init_params (dvi_document);
605 dvi_document->exporter_filename = NULL;
606 dvi_document->exporter_opts = NULL;