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"
28 #include "pixbuf-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 */
61 typedef struct _DviDocumentClass DviDocumentClass;
63 static void dvi_document_document_iface_init (EvDocumentIface *iface);
64 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
65 static void dvi_document_get_page_size (EvDocument *document,
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_pixbuf_device_init (&dvi_document->context->device);
112 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
113 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
115 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
116 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
118 g_free (dvi_document->uri);
119 dvi_document->uri = g_strdup (uri);
126 dvi_document_save (EvDocument *document,
130 DviDocument *dvi_document = DVI_DOCUMENT (document);
132 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
136 dvi_document_get_n_pages (EvDocument *document)
138 DviDocument *dvi_document = DVI_DOCUMENT (document);
139 return dvi_document->context->npages;
143 dvi_document_get_page_size (EvDocument *document,
148 DviDocument * dvi_document = DVI_DOCUMENT (document);
150 *width = dvi_document->base_width;
151 *height = dvi_document->base_height;;
157 dvi_document_render_pixbuf (EvDocument *document,
161 GdkPixbuf *rotated_pixbuf;
163 DviDocument *dvi_document = DVI_DOCUMENT(document);
165 gint required_width, required_height;
166 gint proposed_width, proposed_height;
167 gint xmargin = 0, ymargin = 0;
169 /* We should protect our context since it's not
170 * thread safe. The work to the future -
171 * let context render page independently
173 g_mutex_lock (dvi_context_mutex);
175 mdvi_setpage(dvi_document->context, rc->page);
177 mdvi_set_shrink (dvi_document->context,
178 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
179 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
181 required_width = dvi_document->base_width * rc->scale;
182 required_height = dvi_document->base_height * rc->scale;
183 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
184 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
186 if (required_width >= proposed_width)
187 xmargin = (required_width - proposed_width) / 2;
188 if (required_height >= proposed_height)
189 ymargin = (required_height - proposed_height) / 2;
191 mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
193 mdvi_pixbuf_device_render (dvi_document->context);
195 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
197 g_mutex_unlock (dvi_context_mutex);
199 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
200 g_object_unref (pixbuf);
202 return rotated_pixbuf;
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)
213 mdvi_pixbuf_device_free (&dvi_document->context->device);
214 mdvi_destroy_context (dvi_document->context);
216 g_mutex_unlock (dvi_context_mutex);
218 if (dvi_document->params)
219 g_free (dvi_document->params);
221 g_free (dvi_document->uri);
223 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
228 dvi_document_class_init (DviDocumentClass *klass)
230 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
232 gobject_class->finalize = dvi_document_finalize;
234 mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
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_pixbuf = dvi_document_render_pixbuf;
265 iface->get_info = dvi_document_get_info;
269 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
271 gint suggested_width,
275 DviDocument *dvi_document = DVI_DOCUMENT (document);
278 page_ratio = dvi_document->base_height / dvi_document->base_width;
279 *width = suggested_width;
280 *height = (gint) (suggested_width * page_ratio);
286 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
292 DviDocument *dvi_document = DVI_DOCUMENT (document);
294 GdkPixbuf *border_pixbuf;
295 GdkPixbuf *rotated_pixbuf;
296 gint thumb_width, thumb_height;
297 gint proposed_width, proposed_height;
299 dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
301 g_mutex_lock (dvi_context_mutex);
303 mdvi_setpage(dvi_document->context, page);
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_pixbuf_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_pixbuf_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);
323 mdvi_pixbuf_device_render (dvi_document->context);
324 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
326 g_mutex_unlock (dvi_context_mutex);
328 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation);
329 g_object_unref (pixbuf);
332 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
333 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
334 g_object_unref (tmp_pixbuf);
337 return rotated_pixbuf;
341 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
343 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
344 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
349 dvi_document_init_params (DviDocument *dvi_document)
351 dvi_document->params = g_new0 (DviParams, 1);
353 dvi_document->params->dpi = MDVI_DPI;
354 dvi_document->params->vdpi = MDVI_VDPI;
355 dvi_document->params->mag = MDVI_MAGNIFICATION;
356 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
357 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
358 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
359 dvi_document->params->hdrift = 0;
360 dvi_document->params->vdrift = 0;
361 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
362 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
363 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
365 dvi_document->spec = NULL;
367 dvi_document->params->bg = 0xffffffff;
368 dvi_document->params->fg = 0xff000000;
372 dvi_document_init (DviDocument *dvi_document)
374 dvi_document->context = NULL;
375 dvi_document_init_params (dvi_document);