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 "pixbuf-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 */
59 typedef struct _DviDocumentClass DviDocumentClass;
61 static void dvi_document_document_iface_init (EvDocumentIface *iface);
62 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
63 static void dvi_document_get_page_size (EvDocument *document,
68 G_DEFINE_TYPE_WITH_CODE
69 (DviDocument, dvi_document, G_TYPE_OBJECT,
71 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);
72 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
76 dvi_document_load (EvDocument *document,
81 DviDocument *dvi_document = DVI_DOCUMENT(document);
83 filename = g_filename_from_uri (uri, NULL, error);
88 EV_DOCUMENT_ERROR_INVALID,
89 _("File not available"));
93 g_mutex_lock (dvi_context_mutex);
94 if (dvi_document->context)
95 mdvi_destroy_context (dvi_document->context);
97 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
98 g_mutex_unlock (dvi_context_mutex);
100 if (!dvi_document->context) {
103 EV_DOCUMENT_ERROR_INVALID,
104 _("DVI document has incorrect format"));
108 mdvi_pixbuf_device_init (&dvi_document->context->device);
110 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
111 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
113 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
114 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
116 g_free (dvi_document->uri);
117 dvi_document->uri = g_strdup (uri);
124 dvi_document_save (EvDocument *document,
128 DviDocument *dvi_document = DVI_DOCUMENT (document);
130 return ev_xfer_uri_simple (dvi_document->uri, uri, error);
134 dvi_document_get_n_pages (EvDocument *document)
136 DviDocument *dvi_document = DVI_DOCUMENT (document);
137 return dvi_document->context->npages;
141 dvi_document_get_page_size (EvDocument *document,
146 DviDocument * dvi_document = DVI_DOCUMENT (document);
148 *width = dvi_document->base_width;
149 *height = dvi_document->base_height;;
155 dvi_document_render_pixbuf (EvDocument *document,
159 GdkPixbuf *rotated_pixbuf;
161 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;
180 required_height = dvi_document->base_height * rc->scale;
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_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
191 mdvi_pixbuf_device_render (dvi_document->context);
193 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
195 g_mutex_unlock (dvi_context_mutex);
197 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
198 g_object_unref (pixbuf);
200 return rotated_pixbuf;
204 dvi_document_finalize (GObject *object)
206 DviDocument *dvi_document = DVI_DOCUMENT(object);
208 g_mutex_lock (dvi_context_mutex);
209 if (dvi_document->context)
211 mdvi_pixbuf_device_free (&dvi_document->context->device);
212 mdvi_destroy_context (dvi_document->context);
214 g_mutex_unlock (dvi_context_mutex);
216 if (dvi_document->params)
217 g_free (dvi_document->params);
219 g_free (dvi_document->uri);
221 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
226 dvi_document_class_init (DviDocumentClass *klass)
228 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
230 gobject_class->finalize = dvi_document_finalize;
232 mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
233 mdvi_register_fonts ();
235 dvi_context_mutex = g_mutex_new ();
239 dvi_document_can_get_text (EvDocument *document)
244 static EvDocumentInfo *
245 dvi_document_get_info (EvDocument *document)
247 EvDocumentInfo *info;
249 info = g_new0 (EvDocumentInfo, 1);
255 dvi_document_document_iface_init (EvDocumentIface *iface)
257 iface->load = dvi_document_load;
258 iface->save = dvi_document_save;
259 iface->can_get_text = dvi_document_can_get_text;
260 iface->get_n_pages = dvi_document_get_n_pages;
261 iface->get_page_size = dvi_document_get_page_size;
262 iface->render_pixbuf = dvi_document_render_pixbuf;
263 iface->get_info = dvi_document_get_info;
267 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
272 DviDocument *dvi_document = DVI_DOCUMENT (document);
273 gdouble page_width = dvi_document->base_width;
274 gdouble page_height = dvi_document->base_height;
276 if (rc->rotation == 90 || rc->rotation == 270) {
277 *width = (gint) (page_height * rc->scale);
278 *height = (gint) (page_width * rc->scale);
280 *width = (gint) (page_width * rc->scale);
281 *height = (gint) (page_height * rc->scale);
286 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
290 DviDocument *dvi_document = DVI_DOCUMENT (document);
292 GdkPixbuf *border_pixbuf;
293 GdkPixbuf *rotated_pixbuf;
294 gint thumb_width, thumb_height;
295 gint proposed_width, proposed_height;
297 thumb_width = (gint) (dvi_document->base_width * rc->scale);
298 thumb_height = (gint) (dvi_document->base_height * rc->scale);
300 g_mutex_lock (dvi_context_mutex);
302 mdvi_setpage (dvi_document->context, rc->page);
304 mdvi_set_shrink (dvi_document->context,
305 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
306 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
308 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
309 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
312 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
313 MAX (thumb_width - proposed_width, 0) / 2,
314 MAX (thumb_height - proposed_height, 0) / 2);
316 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
317 MAX (thumb_width - proposed_width - 2, 0) / 2,
318 MAX (thumb_height - proposed_height - 2, 0) / 2);
322 mdvi_pixbuf_device_render (dvi_document->context);
323 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
325 g_mutex_unlock (dvi_context_mutex);
327 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
328 g_object_unref (pixbuf);
331 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
333 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 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);