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-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 # define WIFEXITED(x) ((x) != 3)
37 # define WEXITSTATUS(x) (x)
39 # include <sys/wait.h>
43 GMutex *dvi_context_mutex = NULL;
50 struct _DviDocumentClass
52 EvDocumentClass parent_class;
57 EvDocument parent_instance;
63 /* To let document scale we should remember width and height */
70 gchar *exporter_filename;
71 GString *exporter_opts;
74 typedef struct _DviDocumentClass DviDocumentClass;
76 static void dvi_document_file_exporter_iface_init (EvFileExporterInterface *iface);
77 static void dvi_document_do_color_special (DviContext *dvi,
81 EV_BACKEND_REGISTER_WITH_CODE (DviDocument, dvi_document,
83 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
87 dvi_document_load (EvDocument *document,
92 DviDocument *dvi_document = DVI_DOCUMENT(document);
94 filename = g_filename_from_uri (uri, NULL, error);
98 g_mutex_lock (dvi_context_mutex);
99 if (dvi_document->context)
100 mdvi_destroy_context (dvi_document->context);
102 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
103 g_mutex_unlock (dvi_context_mutex);
106 if (!dvi_document->context) {
107 g_set_error_literal (error,
109 EV_DOCUMENT_ERROR_INVALID,
110 _("DVI document has incorrect format"));
114 mdvi_cairo_device_init (&dvi_document->context->device);
117 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
118 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
120 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
121 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
123 g_free (dvi_document->uri);
124 dvi_document->uri = g_strdup (uri);
131 dvi_document_save (EvDocument *document,
135 DviDocument *dvi_document = DVI_DOCUMENT (document);
137 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
141 dvi_document_get_n_pages (EvDocument *document)
143 DviDocument *dvi_document = DVI_DOCUMENT (document);
145 return dvi_document->context->npages;
149 dvi_document_get_page_size (EvDocument *document,
154 DviDocument *dvi_document = DVI_DOCUMENT (document);
156 *width = dvi_document->base_width;
157 *height = dvi_document->base_height;;
160 static cairo_surface_t *
161 dvi_document_render (EvDocument *document,
164 cairo_surface_t *surface;
165 cairo_surface_t *rotated_surface;
166 DviDocument *dvi_document = DVI_DOCUMENT(document);
167 gint required_width, required_height;
168 gint proposed_width, proposed_height;
169 gint xmargin = 0, ymargin = 0;
171 /* We should protect our context since it's not
172 * thread safe. The work to the future -
173 * let context render page independently
175 g_mutex_lock (dvi_context_mutex);
177 mdvi_setpage (dvi_document->context, rc->page->index);
179 mdvi_set_shrink (dvi_document->context,
180 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
181 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
183 required_width = dvi_document->base_width * rc->scale + 0.5;
184 required_height = dvi_document->base_height * rc->scale + 0.5;
185 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
186 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
188 if (required_width >= proposed_width)
189 xmargin = (required_width - proposed_width) / 2;
190 if (required_height >= proposed_height)
191 ymargin = (required_height - proposed_height) / 2;
193 mdvi_cairo_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
194 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
195 mdvi_cairo_device_render (dvi_document->context);
196 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
198 g_mutex_unlock (dvi_context_mutex);
200 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
204 cairo_surface_destroy (surface);
206 return rotated_surface;
210 dvi_document_finalize (GObject *object)
212 DviDocument *dvi_document = DVI_DOCUMENT(object);
214 g_mutex_lock (dvi_context_mutex);
215 if (dvi_document->context) {
216 mdvi_cairo_device_free (&dvi_document->context->device);
217 mdvi_destroy_context (dvi_document->context);
219 g_mutex_unlock (dvi_context_mutex);
221 if (dvi_document->params)
222 g_free (dvi_document->params);
224 if (dvi_document->exporter_filename)
225 g_free (dvi_document->exporter_filename);
227 if (dvi_document->exporter_opts)
228 g_string_free (dvi_document->exporter_opts, TRUE);
230 g_free (dvi_document->uri);
232 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
236 dvi_document_support_synctex (EvDocument *document)
242 dvi_document_class_init (DviDocumentClass *klass)
244 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
245 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
248 gobject_class->finalize = dvi_document_finalize;
250 texmfcnf = get_texmfcnf();
251 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI, texmfcnf);
254 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
255 mdvi_register_fonts ();
257 dvi_context_mutex = g_mutex_new ();
259 ev_document_class->load = dvi_document_load;
260 ev_document_class->save = dvi_document_save;
261 ev_document_class->get_n_pages = dvi_document_get_n_pages;
262 ev_document_class->get_page_size = dvi_document_get_page_size;
263 ev_document_class->render = dvi_document_render;
264 ev_document_class->support_synctex = dvi_document_support_synctex;
267 /* EvFileExporterIface */
269 dvi_document_file_exporter_begin (EvFileExporter *exporter,
270 EvFileExporterContext *fc)
272 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
274 if (dvi_document->exporter_filename)
275 g_free (dvi_document->exporter_filename);
276 dvi_document->exporter_filename = g_strdup (fc->filename);
278 if (dvi_document->exporter_opts) {
279 g_string_free (dvi_document->exporter_opts, TRUE);
281 dvi_document->exporter_opts = g_string_new ("-s ");
285 dvi_document_file_exporter_do_page (EvFileExporter *exporter,
288 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
290 g_string_append_printf (dvi_document->exporter_opts, "%d,", (rc->page->index) + 1);
294 dvi_document_file_exporter_end (EvFileExporter *exporter)
301 DviDocument *dvi_document = DVI_DOCUMENT(exporter);
303 command_line = g_strdup_printf ("dvipdfm %s -o %s \"%s\"", /* dvipdfm -s 1,2,.., -o exporter_filename dvi_filename */
304 dvi_document->exporter_opts->str,
305 dvi_document->exporter_filename,
306 dvi_document->context->filename);
308 success = g_spawn_command_line_sync (command_line,
314 g_free (command_line);
316 if (success == FALSE) {
317 g_warning ("Error: %s", err->message);
318 } else if (!WIFEXITED(exit_stat) || WEXITSTATUS(exit_stat) != EXIT_SUCCESS){
319 g_warning ("Error: dvipdfm does not end normally or exit with a failure status.");
326 static EvFileExporterCapabilities
327 dvi_document_file_exporter_get_capabilities (EvFileExporter *exporter)
329 return EV_FILE_EXPORTER_CAN_PAGE_SET |
330 EV_FILE_EXPORTER_CAN_COPIES |
331 EV_FILE_EXPORTER_CAN_COLLATE |
332 EV_FILE_EXPORTER_CAN_REVERSE |
333 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
337 dvi_document_file_exporter_iface_init (EvFileExporterInterface *iface)
339 iface->begin = dvi_document_file_exporter_begin;
340 iface->do_page = dvi_document_file_exporter_do_page;
341 iface->end = dvi_document_file_exporter_end;
342 iface->get_capabilities = dvi_document_file_exporter_get_capabilities;
345 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
348 hsb2rgb (float h, float s, float v, guchar *red, guchar *green, guchar *blue)
350 float i, f, p, q, t, r, g, b;
354 else if ((h > 360) || (h < 0))
363 q = v * (1 - (s * f));
364 t = v * (1 - (s * (1 - f)));
392 *red = (guchar)floor(r * 255.0);
393 *green = (guchar)floor(g * 255.0);
394 *blue = (guchar)floor(b * 255.0);
400 parse_color (const gchar *ptr,
404 gchar *p = (gchar *)ptr;
407 for (i = 0; i < n_color; i++) {
408 while (isspace (*p)) p++;
409 color[i] = g_ascii_strtod (p, NULL);
410 while (!isspace (*p) && *p != '\0') p++;
417 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
419 if (strncmp (arg, "pop", 3) == 0) {
420 mdvi_pop_color (dvi);
421 } else if (strncmp (arg, "push", 4) == 0) {
422 /* Find color source: Named, CMYK or RGB */
423 const char *tmp = arg + 4;
425 while (isspace (*tmp)) tmp++;
427 if (!strncmp ("rgb", tmp, 3)) {
429 guchar red, green, blue;
431 parse_color (tmp + 4, rgb, 3);
434 green = 255 * rgb[1];
437 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
438 } else if (!strncmp ("hsb", tmp, 4)) {
440 guchar red, green, blue;
442 parse_color (tmp + 4, hsb, 3);
444 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
445 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
446 } else if (!strncmp ("cmyk", tmp, 4)) {
449 guchar red, green, blue;
451 parse_color (tmp + 5, cmyk, 4);
453 r = 1.0 - cmyk[0] - cmyk[3];
456 g = 1.0 - cmyk[1] - cmyk[3];
459 b = 1.0 - cmyk[2] - cmyk[3];
464 green = g * 255 + 0.5;
465 blue = b * 255 + 0.5;
467 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
468 } else if (!strncmp ("gray ", tmp, 5)) {
472 parse_color (tmp + 5, &gray, 1);
474 rgb = gray * 255 + 0.5;
476 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
480 if (gdk_color_parse (tmp, &color)) {
481 guchar red, green, blue;
483 red = color.red * 255 / 65535.;
484 green = color.green * 255 / 65535.;
485 blue = color.blue * 255 / 65535.;
487 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
494 dvi_document_init_params (DviDocument *dvi_document)
496 dvi_document->params = g_new0 (DviParams, 1);
498 dvi_document->params->dpi = MDVI_DPI;
499 dvi_document->params->vdpi = MDVI_VDPI;
500 dvi_document->params->mag = MDVI_MAGNIFICATION;
501 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
502 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
503 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
504 dvi_document->params->hdrift = 0;
505 dvi_document->params->vdrift = 0;
506 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
507 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
508 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
510 dvi_document->spec = NULL;
512 dvi_document->params->bg = 0xffffffff;
513 dvi_document->params->fg = 0xff000000;
517 dvi_document_init (DviDocument *dvi_document)
519 dvi_document->context = NULL;
520 dvi_document_init_params (dvi_document);
522 dvi_document->exporter_filename = NULL;
523 dvi_document->exporter_opts = NULL;