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 */
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 if (dvi_document->context)
94 mdvi_destroy_context (dvi_document->context);
96 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
98 if (!dvi_document->context) {
101 EV_DOCUMENT_ERROR_INVALID,
102 _("DVI document has incorrect format"));
106 mdvi_pixbuf_device_init (&dvi_document->context->device);
108 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
109 + 2 * unit2pix(dvi_document->params->dpi, MDVI_HMARGIN) / dvi_document->params->hshrink;
111 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
112 + 2 * unit2pix(dvi_document->params->vdpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
114 dvi_context_mutex = g_mutex_new ();
122 dvi_document_save (EvDocument *document,
126 g_warning ("dvi_document_save not implemented"); /* FIXME */
131 dvi_document_get_n_pages (EvDocument *document)
133 DviDocument *dvi_document = DVI_DOCUMENT (document);
134 return dvi_document->context->npages;
138 dvi_document_get_page_size (EvDocument *document,
143 DviDocument * dvi_document = DVI_DOCUMENT (document);
145 *width = dvi_document->base_width;
146 *height = dvi_document->base_height;;
152 dvi_document_render_pixbuf (EvDocument *document,
156 GdkPixbuf *rotated_pixbuf;
158 DviDocument *dvi_document = DVI_DOCUMENT(document);
160 gint required_width, required_height;
161 gint proposed_width, proposed_height;
162 gint xmargin = 0, ymargin = 0;
164 /* We should protect our context since it's not
165 * thread safe. The work to the future -
166 * let context render page independently
168 g_mutex_lock (dvi_context_mutex);
170 mdvi_setpage(dvi_document->context, rc->page);
172 mdvi_set_shrink (dvi_document->context,
173 (int)((dvi_document->params->hshrink - 1) / rc->scale) + 1,
174 (int)((dvi_document->params->vshrink - 1) / rc->scale) + 1);
176 required_width = dvi_document->base_width * rc->scale;
177 required_height = dvi_document->base_height * rc->scale;
178 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
179 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
181 if (required_width >= proposed_width)
182 xmargin = (required_width - proposed_width) / 2;
183 if (required_height >= proposed_height)
184 ymargin = (required_height - proposed_height) / 2;
186 mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
188 mdvi_pixbuf_device_render (dvi_document->context);
190 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
192 g_mutex_unlock (dvi_context_mutex);
194 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
195 g_object_unref (pixbuf);
197 return rotated_pixbuf;
201 dvi_document_finalize (GObject *object)
203 DviDocument *dvi_document = DVI_DOCUMENT(object);
205 if (dvi_document->context)
207 mdvi_pixbuf_device_free (&dvi_document->context->device);
208 mdvi_destroy_context (dvi_document->context);
211 if (dvi_document->params)
212 g_free (dvi_document->params);
214 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
219 dvi_document_class_init (DviDocumentClass *klass)
221 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
223 gobject_class->finalize = dvi_document_finalize;
227 dvi_document_can_get_text (EvDocument *document)
232 static EvDocumentInfo *
233 dvi_document_get_info (EvDocument *document)
235 EvDocumentInfo *info;
237 info = g_new0 (EvDocumentInfo, 1);
243 dvi_document_document_iface_init (EvDocumentIface *iface)
245 iface->load = dvi_document_load;
246 iface->save = dvi_document_save;
247 iface->can_get_text = dvi_document_can_get_text;
248 iface->get_n_pages = dvi_document_get_n_pages;
249 iface->get_page_size = dvi_document_get_page_size;
250 iface->render_pixbuf = dvi_document_render_pixbuf;
251 iface->get_info = dvi_document_get_info;
255 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
257 gint suggested_width,
261 DviDocument *dvi_document = DVI_DOCUMENT (document);
264 page_ratio = dvi_document->base_height / dvi_document->base_width;
265 *width = suggested_width;
266 *height = (gint) (suggested_width * page_ratio);
272 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
278 DviDocument *dvi_document = DVI_DOCUMENT (document);
280 GdkPixbuf *border_pixbuf;
281 GdkPixbuf *rotated_pixbuf;
282 gint thumb_width, thumb_height;
283 gint proposed_width, proposed_height;
285 dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
287 g_mutex_lock (dvi_context_mutex);
289 mdvi_setpage(dvi_document->context, page);
291 mdvi_set_shrink (dvi_document->context,
292 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
293 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
295 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
296 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
299 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
300 MAX (thumb_width - proposed_width, 0) / 2,
301 MAX (thumb_height - proposed_height, 0) / 2);
303 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
304 MAX (thumb_width - proposed_width - 2, 0) / 2,
305 MAX (thumb_height - proposed_height - 2, 0) / 2);
309 mdvi_pixbuf_device_render (dvi_document->context);
310 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
312 g_mutex_unlock (dvi_context_mutex);
314 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation);
315 g_object_unref (pixbuf);
318 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
319 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
320 g_object_unref (tmp_pixbuf);
323 return rotated_pixbuf;
327 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
329 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
330 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
335 dvi_document_init_params (DviDocument *dvi_document)
337 dvi_document->params = g_new0 (DviParams, 1);
339 dvi_document->params->dpi = MDVI_DPI;
340 dvi_document->params->vdpi = MDVI_VDPI;
341 dvi_document->params->mag = MDVI_MAGNIFICATION;
342 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
343 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
344 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
345 dvi_document->params->hdrift = 0;
346 dvi_document->params->vdrift = 0;
347 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
348 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
349 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
351 dvi_document->spec = NULL;
353 dvi_document->params->bg = 0xffffffff;
354 dvi_document->params->fg = 0xff000000;
356 mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
358 mdvi_register_fonts ();
362 dvi_document_init (DviDocument *dvi_document)
364 dvi_document->context = NULL;
365 dvi_document_init_params (dvi_document);