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.
20 #include "dvi-document.h"
21 #include "ev-document-thumbnails.h"
22 #include "ev-document-misc.h"
23 #include "ev-file-exporter.h"
27 #include "cairo-device.h"
30 #include <glib/gi18n.h>
32 GMutex *dvi_context_mutex = NULL;
39 struct _DviDocumentClass
41 GObjectClass parent_class;
46 GObject parent_instance;
52 /* To let document scale we should remember width and height */
59 gchar *exporter_filename;
60 GString *exporter_opts;
63 typedef struct _DviDocumentClass DviDocumentClass;
65 static void dvi_document_document_iface_init (EvDocumentIface *iface);
66 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
67 static void dvi_document_file_exporter_iface_init (EvFileExporterIface *iface);
68 static void dvi_document_get_page_size (EvDocument *document,
72 static void dvi_document_do_color_special (DviContext *dvi,
76 G_DEFINE_TYPE_WITH_CODE
77 (DviDocument, dvi_document, G_TYPE_OBJECT,
79 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);
80 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
81 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
85 dvi_document_load (EvDocument *document,
90 DviDocument *dvi_document = DVI_DOCUMENT(document);
92 filename = g_filename_from_uri (uri, NULL, error);
97 EV_DOCUMENT_ERROR_INVALID,
98 _("File not available"));
102 g_mutex_lock (dvi_context_mutex);
103 if (dvi_document->context)
104 mdvi_destroy_context (dvi_document->context);
106 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
107 g_mutex_unlock (dvi_context_mutex);
109 if (!dvi_document->context) {
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);
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);
240 dvi_document_class_init (DviDocumentClass *klass)
242 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
244 gobject_class->finalize = dvi_document_finalize;
246 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
247 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
248 mdvi_register_fonts ();
250 dvi_context_mutex = g_mutex_new ();
254 dvi_document_can_get_text (EvDocument *document)
259 static EvDocumentInfo *
260 dvi_document_get_info (EvDocument *document)
262 EvDocumentInfo *info;
264 info = g_new0 (EvDocumentInfo, 1);
270 dvi_document_document_iface_init (EvDocumentIface *iface)
272 iface->load = dvi_document_load;
273 iface->save = dvi_document_save;
274 iface->can_get_text = dvi_document_can_get_text;
275 iface->get_n_pages = dvi_document_get_n_pages;
276 iface->get_page_size = dvi_document_get_page_size;
277 iface->render = dvi_document_render;
278 iface->get_info = dvi_document_get_info;
282 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
287 DviDocument *dvi_document = DVI_DOCUMENT (document);
288 gdouble page_width = dvi_document->base_width;
289 gdouble page_height = dvi_document->base_height;
291 if (rc->rotation == 90 || rc->rotation == 270) {
292 *width = (gint) (page_height * rc->scale);
293 *height = (gint) (page_width * rc->scale);
295 *width = (gint) (page_width * rc->scale);
296 *height = (gint) (page_height * rc->scale);
301 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
305 DviDocument *dvi_document = DVI_DOCUMENT (document);
307 GdkPixbuf *rotated_pixbuf;
308 cairo_surface_t *surface;
309 gint thumb_width, thumb_height;
310 gint proposed_width, proposed_height;
312 thumb_width = (gint) (dvi_document->base_width * rc->scale);
313 thumb_height = (gint) (dvi_document->base_height * rc->scale);
315 g_mutex_lock (dvi_context_mutex);
317 mdvi_setpage (dvi_document->context, rc->page);
319 mdvi_set_shrink (dvi_document->context,
320 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
321 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
323 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
324 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
327 mdvi_cairo_device_set_margins (&dvi_document->context->device,
328 MAX (thumb_width - proposed_width, 0) / 2,
329 MAX (thumb_height - proposed_height, 0) / 2);
331 mdvi_cairo_device_set_margins (&dvi_document->context->device,
332 MAX (thumb_width - proposed_width - 2, 0) / 2,
333 MAX (thumb_height - proposed_height - 2, 0) / 2);
336 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
337 mdvi_cairo_device_render (dvi_document->context);
338 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
339 g_mutex_unlock (dvi_context_mutex);
341 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
342 cairo_surface_destroy (surface);
344 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
345 g_object_unref (pixbuf);
348 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
350 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
351 g_object_unref (tmp_pixbuf);
354 return rotated_pixbuf;
358 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
360 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
361 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
364 /* EvFileExporterIface */
366 dvi_document_file_exporter_format_supported (EvFileExporter *exporter,
367 EvFileExporterFormat format)
369 return (format == EV_FILE_FORMAT_PDF); /* only exporting to PDF is implemented. */
373 dvi_document_file_exporter_begin (EvFileExporter *exporter,
374 EvFileExporterFormat format,
375 const char *filename, /* for storing the temp pdf file */
382 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
384 if (dvi_document->exporter_filename)
385 g_free (dvi_document->exporter_filename);
386 dvi_document->exporter_filename = g_strdup(filename);
388 if (dvi_document->exporter_opts) {
389 g_string_free (dvi_document->exporter_opts, TRUE);
391 dvi_document->exporter_opts = g_string_new ("-s ");
395 dvi_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
397 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
399 g_string_append_printf(dvi_document->exporter_opts, "%d,", (rc->page)+1);
403 dvi_document_file_exporter_end (EvFileExporter *exporter)
410 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
412 command_line = g_strdup_printf ("dvipdfm %s -o %s %s", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
413 dvi_document->exporter_opts->str,
414 dvi_document->exporter_filename,
415 dvi_document->context->filename);
417 success = g_spawn_command_line_sync (command_line,
423 g_free(command_line);
425 if (success == FALSE) {
426 g_warning (_("Error: %s"), err->message);
427 } else if (exit_stat != 0) {
428 g_warning (_("Error: dvipdfm exited with non-zero status."));
436 dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
438 iface->format_supported = dvi_document_file_exporter_format_supported;
439 iface->begin = dvi_document_file_exporter_begin;
440 iface->do_page = dvi_document_file_exporter_do_page;
441 iface->end = dvi_document_file_exporter_end;
444 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
447 hsb2rgb (float h, float s, float v, char *red, char *green, char *blue)
449 float i, f, p, q, t, r, g, b;
453 else if ((h > 360) || (h < 0))
462 q = v * (1 - (s * f));
463 t = v * (1 - (s * (1 - f)));
491 (*red) = (char)floor(r * 255);
492 (*green) = (char)floor(g * 255);
493 (*blue) = (char)floor(b * 255);
499 parse_color (const gchar *ptr,
503 gchar *p = (gchar *)ptr;
506 for (i = 0; i < n_color; i++) {
507 while (isspace (*p)) p++;
508 color[i] = g_ascii_strtod (p, NULL);
509 while (!isspace (*p) && *p != '\0') p++;
516 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
518 if (strncmp (arg, "pop", 3) == 0) {
519 mdvi_pop_color (dvi);
520 } else if (strncmp (arg, "push", 4) == 0) {
521 /* Find color source: Named, CMYK or RGB */
522 const char *tmp = arg + 4;
524 while (isspace (*tmp)) tmp++;
526 if (!strncmp ("rgb", tmp, 3)) {
528 guchar red, green, blue;
530 parse_color (tmp + 4, rgb, 3);
533 green = 255 * rgb[1];
536 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
537 } else if (!strncmp ("hsb", tmp, 4)) {
539 guchar red, green, blue;
541 parse_color (tmp + 4, hsb, 3);
543 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
544 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
545 } else if (!strncmp ("cmyk", tmp, 4)) {
548 guchar red, green, blue;
550 parse_color (tmp + 5, cmyk, 4);
552 r = 1.0 - cmyk[0] - cmyk[3];
555 g = 1.0 - cmyk[1] - cmyk[3];
558 b = 1.0 - cmyk[2] - cmyk[3];
563 green = g * 255 + 0.5;
564 blue = b * 255 + 0.5;
566 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
567 } else if (!strncmp ("gray ", tmp, 5)) {
571 parse_color (tmp + 5, &gray, 1);
573 rgb = gray * 255 + 0.5;
575 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
579 if (gdk_color_parse (tmp, &color)) {
580 guchar red, green, blue;
582 red = color.red * 255 / 65535.;
583 green = color.green * 255 / 65535.;
584 blue = color.blue * 255 / 65535.;
586 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
593 dvi_document_init_params (DviDocument *dvi_document)
595 dvi_document->params = g_new0 (DviParams, 1);
597 dvi_document->params->dpi = MDVI_DPI;
598 dvi_document->params->vdpi = MDVI_VDPI;
599 dvi_document->params->mag = MDVI_MAGNIFICATION;
600 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
601 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
602 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
603 dvi_document->params->hdrift = 0;
604 dvi_document->params->vdrift = 0;
605 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
606 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
607 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
609 dvi_document->spec = NULL;
611 dvi_document->params->bg = 0xffffffff;
612 dvi_document->params->fg = 0xff000000;
616 dvi_document_init (DviDocument *dvi_document)
618 dvi_document->context = NULL;
619 dvi_document_init_params (dvi_document);
621 dvi_document->exporter_filename = NULL;
622 dvi_document->exporter_opts = NULL;