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"
26 #include "cairo-device.h"
29 #include <glib/gi18n.h>
31 GMutex *dvi_context_mutex = NULL;
38 struct _DviDocumentClass
40 GObjectClass parent_class;
45 GObject parent_instance;
51 /* To let document scale we should remember width and height */
58 typedef struct _DviDocumentClass DviDocumentClass;
60 static void dvi_document_document_iface_init (EvDocumentIface *iface);
61 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
62 static void dvi_document_get_page_size (EvDocument *document,
66 static void dvi_document_do_color_special (DviContext *dvi,
70 G_DEFINE_TYPE_WITH_CODE
71 (DviDocument, dvi_document, G_TYPE_OBJECT,
73 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);
74 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
78 dvi_document_load (EvDocument *document,
83 DviDocument *dvi_document = DVI_DOCUMENT(document);
85 filename = g_filename_from_uri (uri, NULL, error);
90 EV_DOCUMENT_ERROR_INVALID,
91 _("File not available"));
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);
102 if (!dvi_document->context) {
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);
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 g_free (dvi_document->uri);
222 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
227 dvi_document_class_init (DviDocumentClass *klass)
229 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231 gobject_class->finalize = dvi_document_finalize;
233 mdvi_init_kpathsea ("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
234 mdvi_register_special ("Color", "color", NULL, dvi_document_do_color_special, 1);
235 mdvi_register_fonts ();
237 dvi_context_mutex = g_mutex_new ();
241 dvi_document_can_get_text (EvDocument *document)
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->can_get_text = dvi_document_can_get_text;
262 iface->get_n_pages = dvi_document_get_n_pages;
263 iface->get_page_size = dvi_document_get_page_size;
264 iface->render = dvi_document_render;
265 iface->get_info = dvi_document_get_info;
269 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
274 DviDocument *dvi_document = DVI_DOCUMENT (document);
275 gdouble page_width = dvi_document->base_width;
276 gdouble page_height = dvi_document->base_height;
278 if (rc->rotation == 90 || rc->rotation == 270) {
279 *width = (gint) (page_height * rc->scale);
280 *height = (gint) (page_width * rc->scale);
282 *width = (gint) (page_width * rc->scale);
283 *height = (gint) (page_height * rc->scale);
288 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
292 DviDocument *dvi_document = DVI_DOCUMENT (document);
294 GdkPixbuf *rotated_pixbuf;
295 cairo_surface_t *surface;
296 gint thumb_width, thumb_height;
297 gint proposed_width, proposed_height;
299 thumb_width = (gint) (dvi_document->base_width * rc->scale);
300 thumb_height = (gint) (dvi_document->base_height * rc->scale);
302 g_mutex_lock (dvi_context_mutex);
304 mdvi_setpage (dvi_document->context, rc->page);
306 mdvi_set_shrink (dvi_document->context,
307 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
308 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
310 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
311 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
314 mdvi_cairo_device_set_margins (&dvi_document->context->device,
315 MAX (thumb_width - proposed_width, 0) / 2,
316 MAX (thumb_height - proposed_height, 0) / 2);
318 mdvi_cairo_device_set_margins (&dvi_document->context->device,
319 MAX (thumb_width - proposed_width - 2, 0) / 2,
320 MAX (thumb_height - proposed_height - 2, 0) / 2);
323 mdvi_cairo_device_set_scale (&dvi_document->context->device, rc->scale);
324 mdvi_cairo_device_render (dvi_document->context);
325 surface = mdvi_cairo_device_get_surface (&dvi_document->context->device);
326 g_mutex_unlock (dvi_context_mutex);
328 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
329 cairo_surface_destroy (surface);
331 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
332 g_object_unref (pixbuf);
335 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
337 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
338 g_object_unref (tmp_pixbuf);
341 return rotated_pixbuf;
345 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
347 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
348 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
351 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
354 hsb2rgb (float h, float s, float v, char *red, char *green, char *blue)
356 float i, f, p, q, t, r, g, b;
360 else if ((h > 360) || (h < 0))
369 q = v * (1 - (s * f));
370 t = v * (1 - (s * (1 - f)));
398 (*red) = (char)floor(r * 255);
399 (*green) = (char)floor(g * 255);
400 (*blue) = (char)floor(b * 255);
406 parse_color (const gchar *ptr,
410 gchar *p = (gchar *)ptr;
413 for (i = 0; i < n_color; i++) {
414 while (isspace (*p)) p++;
415 color[i] = g_ascii_strtod (p, NULL);
416 while (!isspace (*p) && *p != '\0') p++;
423 dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg)
425 if (strncmp (arg, "pop", 3) == 0) {
426 mdvi_pop_color (dvi);
427 } else if (strncmp (arg, "push", 4) == 0) {
428 /* Find color source: Named, CMYK or RGB */
429 const char *tmp = arg + 4;
431 while (isspace (*tmp)) tmp++;
433 if (!strncmp ("rgb", tmp, 3)) {
435 guchar red, green, blue;
437 parse_color (tmp + 4, rgb, 3);
440 green = 255 * rgb[1];
443 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
444 } else if (!strncmp ("hsb", tmp, 4)) {
446 guchar red, green, blue;
448 parse_color (tmp + 4, hsb, 3);
450 if (hsb2rgb (hsb[0], hsb[1], hsb[2], &red, &green, &blue))
451 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
452 } else if (!strncmp ("cmyk", tmp, 4)) {
455 guchar red, green, blue;
457 parse_color (tmp + 5, cmyk, 4);
459 r = 1.0 - cmyk[0] - cmyk[3];
462 g = 1.0 - cmyk[1] - cmyk[3];
465 b = 1.0 - cmyk[2] - cmyk[3];
470 green = g * 255 + 0.5;
471 blue = b * 255 + 0.5;
473 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
474 } else if (!strncmp ("gray ", tmp, 5)) {
478 parse_color (tmp + 5, &gray, 1);
480 rgb = gray * 255 + 0.5;
482 mdvi_push_color (dvi, RGB2ULONG (rgb, rgb, rgb), 0xFFFFFFFF);
486 if (gdk_color_parse (tmp, &color)) {
487 guchar red, green, blue;
489 red = color.red * 255 / 65535.;
490 green = color.green * 255 / 65535.;
491 blue = color.blue * 255 / 65535.;
493 mdvi_push_color (dvi, RGB2ULONG (red, green, blue), 0xFFFFFFFF);
500 dvi_document_init_params (DviDocument *dvi_document)
502 dvi_document->params = g_new0 (DviParams, 1);
504 dvi_document->params->dpi = MDVI_DPI;
505 dvi_document->params->vdpi = MDVI_VDPI;
506 dvi_document->params->mag = MDVI_MAGNIFICATION;
507 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
508 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
509 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
510 dvi_document->params->hdrift = 0;
511 dvi_document->params->vdrift = 0;
512 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
513 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
514 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
516 dvi_document->spec = NULL;
518 dvi_document->params->bg = 0xffffffff;
519 dvi_document->params->fg = 0xff000000;
523 dvi_document_init (DviDocument *dvi_document)
525 dvi_document->context = NULL;
526 dvi_document_init_params (dvi_document);