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"
32 GMutex *dvi_context_mutex = NULL;
39 struct _DviDocumentClass
41 GObjectClass parent_class;
46 GObject parent_instance;
52 /* 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,
67 G_DEFINE_TYPE_WITH_CODE
68 (DviDocument, dvi_document, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);
71 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
75 dvi_document_load (EvDocument *document,
80 DviDocument *dvi_document = DVI_DOCUMENT(document);
82 filename = g_filename_from_uri (uri, NULL, error);
87 if (dvi_document->context)
88 mdvi_destroy_context (dvi_document->context);
90 dvi_document->context = mdvi_init_context(dvi_document->params, dvi_document->spec, filename);
92 mdvi_pixbuf_device_init (&dvi_document->context->device);
94 dvi_document->base_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv
95 + 2 * unit2pix(dvi_document->params->dpi, MDVI_VMARGIN) / dvi_document->params->hshrink;
97 dvi_document->base_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv
98 + 2 * unit2pix(dvi_document->params->dpi, MDVI_VMARGIN) / dvi_document->params->vshrink;
100 dvi_context_mutex = g_mutex_new ();
108 dvi_document_save (EvDocument *document,
112 g_warning ("dvi_document_save not implemented"); /* FIXME */
117 dvi_document_get_n_pages (EvDocument *document)
119 DviDocument *dvi_document = DVI_DOCUMENT (document);
120 return dvi_document->context->npages;
124 dvi_document_get_page_size (EvDocument *document,
129 DviDocument * dvi_document = DVI_DOCUMENT (document);
132 *width = dvi_document->base_width;
135 *height = dvi_document->base_height;
141 dvi_document_render_pixbuf (EvDocument *document, int page, double scale)
145 DviDocument *dvi_document = DVI_DOCUMENT(document);
147 gint required_width, required_height;
148 gint proposed_width, proposed_height;
149 gint xmargin = 0, ymargin = 0;
151 /* We should protect our context since it's not
152 * thread safe. The work to the future -
153 * let context render page independently
155 g_mutex_lock (dvi_context_mutex);
157 mdvi_setpage(dvi_document->context, page);
159 mdvi_set_shrink (dvi_document->context,
160 (int)((dvi_document->params->hshrink - 1) / scale) + 1,
161 (int)((dvi_document->params->vshrink - 1) / scale) + 1);
163 required_width = dvi_document->base_width * scale;
164 required_height = dvi_document->base_height * scale;
165 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
166 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
168 if (required_width >= proposed_width)
169 xmargin = (required_width - proposed_width) / 2;
170 if (required_height >= proposed_height)
171 ymargin = (required_height - proposed_height) / 2;
173 mdvi_pixbuf_device_set_margins (&dvi_document->context->device, xmargin, ymargin);
175 mdvi_pixbuf_device_render (dvi_document->context);
177 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
179 g_mutex_unlock (dvi_context_mutex);
185 dvi_document_finalize (GObject *object)
187 DviDocument *dvi_document = DVI_DOCUMENT(object);
189 if (dvi_document->context)
191 mdvi_pixbuf_device_free (&dvi_document->context->device);
192 mdvi_destroy_context (dvi_document->context);
195 if (dvi_document->params)
196 g_free (dvi_document->params);
198 G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
202 dvi_document_set_property (GObject *object,
216 dvi_document_get_property (GObject *object,
224 g_value_set_string (value, NULL);
230 dvi_document_class_init (DviDocumentClass *klass)
232 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
234 gobject_class->finalize = dvi_document_finalize;
235 gobject_class->get_property = dvi_document_get_property;
236 gobject_class->set_property = dvi_document_set_property;
238 g_object_class_override_property (gobject_class, PROP_TITLE, "title");
242 dvi_document_get_text (EvDocument *document, gint page, EvRectangle *rect)
244 /* FIXME this method should not be in EvDocument */
245 g_warning ("dvi_document_get_text not implemented");
250 dvi_document_document_iface_init (EvDocumentIface *iface)
252 iface->load = dvi_document_load;
253 iface->save = dvi_document_save;
254 iface->get_text = dvi_document_get_text;
255 iface->get_n_pages = dvi_document_get_n_pages;
256 iface->get_page_size = dvi_document_get_page_size;
257 iface->render_pixbuf = dvi_document_render_pixbuf;
261 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
263 gint suggested_width,
267 DviDocument *dvi_document = DVI_DOCUMENT (document);
270 page_ratio = dvi_document->base_height / dvi_document->base_width;
271 *width = suggested_width;
272 *height = (gint) (suggested_width * page_ratio);
278 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
283 DviDocument *dvi_document = DVI_DOCUMENT (document);
285 GdkPixbuf *border_pixbuf;
286 gint thumb_width, thumb_height;
287 gint proposed_width, proposed_height;
289 dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
291 g_mutex_lock (dvi_context_mutex);
293 mdvi_setpage(dvi_document->context, page);
295 mdvi_set_shrink (dvi_document->context,
296 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
297 (int)dvi_document->base_width * dvi_document->params->vshrink / thumb_height);
299 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
300 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
303 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
304 MAX (thumb_width - proposed_width, 0) / 2,
305 MAX (thumb_height - proposed_height, 0) / 2);
307 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
308 MAX (thumb_width - proposed_width - 2, 0) / 2,
309 MAX (thumb_height - proposed_height - 2, 0) / 2);
313 mdvi_pixbuf_device_render (dvi_document->context);
314 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
316 g_mutex_unlock (dvi_context_mutex);
319 border_pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
320 gdk_pixbuf_copy_area (pixbuf, 0, 0,
321 thumb_width - 2, thumb_height - 2,
322 border_pixbuf, 2, 2);
323 g_object_unref (pixbuf);
324 pixbuf = border_pixbuf;
332 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
334 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
335 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
340 dvi_document_init_params (DviDocument *dvi_document)
342 dvi_document->params = g_new0 (DviParams, 1);
344 dvi_document->params->dpi = MDVI_DPI;
345 dvi_document->params->vdpi = MDVI_VDPI;
346 dvi_document->params->mag = MDVI_MAGNIFICATION;
347 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
348 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
349 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
350 dvi_document->params->hdrift = 0;
351 dvi_document->params->vdrift = 0;
352 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
353 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
354 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
356 dvi_document->spec = NULL;
358 dvi_document->params->bg = 0xffffffff;
359 dvi_document->params->fg = 0xff000000;
361 mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
363 mdvi_register_fonts ();
367 dvi_document_init (DviDocument *dvi_document)
369 dvi_document->context = NULL;
370 dvi_document_init_params (dvi_document);