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_HMARGIN) / 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->vdpi, 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);
203 dvi_document_class_init (DviDocumentClass *klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
207 gobject_class->finalize = dvi_document_finalize;
211 dvi_document_can_get_text (EvDocument *document)
216 static EvDocumentInfo *
217 dvi_document_get_info (EvDocument *document)
219 EvDocumentInfo *info;
221 info = g_new0 (EvDocumentInfo, 1);
227 dvi_document_document_iface_init (EvDocumentIface *iface)
229 iface->load = dvi_document_load;
230 iface->save = dvi_document_save;
231 iface->can_get_text = dvi_document_can_get_text;
232 iface->get_n_pages = dvi_document_get_n_pages;
233 iface->get_page_size = dvi_document_get_page_size;
234 iface->render_pixbuf = dvi_document_render_pixbuf;
235 iface->get_info = dvi_document_get_info;
239 dvi_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
241 gint suggested_width,
245 DviDocument *dvi_document = DVI_DOCUMENT (document);
248 page_ratio = dvi_document->base_height / dvi_document->base_width;
249 *width = suggested_width;
250 *height = (gint) (suggested_width * page_ratio);
256 dvi_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
261 DviDocument *dvi_document = DVI_DOCUMENT (document);
263 GdkPixbuf *border_pixbuf;
264 gint thumb_width, thumb_height;
265 gint proposed_width, proposed_height;
267 dvi_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
269 g_mutex_lock (dvi_context_mutex);
271 mdvi_setpage(dvi_document->context, page);
273 mdvi_set_shrink (dvi_document->context,
274 (int)dvi_document->base_width * dvi_document->params->hshrink / thumb_width,
275 (int)dvi_document->base_height * dvi_document->params->vshrink / thumb_height);
277 proposed_width = dvi_document->context->dvi_page_w * dvi_document->context->params.conv;
278 proposed_height = dvi_document->context->dvi_page_h * dvi_document->context->params.vconv;
281 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
282 MAX (thumb_width - proposed_width, 0) / 2,
283 MAX (thumb_height - proposed_height, 0) / 2);
285 mdvi_pixbuf_device_set_margins (&dvi_document->context->device,
286 MAX (thumb_width - proposed_width - 2, 0) / 2,
287 MAX (thumb_height - proposed_height - 2, 0) / 2);
291 mdvi_pixbuf_device_render (dvi_document->context);
292 pixbuf = mdvi_pixbuf_device_get_pixbuf (&dvi_document->context->device);
294 g_mutex_unlock (dvi_context_mutex);
297 border_pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
298 gdk_pixbuf_copy_area (pixbuf, 0, 0,
299 thumb_width - 2, thumb_height - 2,
300 border_pixbuf, 2, 2);
301 g_object_unref (pixbuf);
302 pixbuf = border_pixbuf;
310 dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
312 iface->get_thumbnail = dvi_document_thumbnails_get_thumbnail;
313 iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
318 dvi_document_init_params (DviDocument *dvi_document)
320 dvi_document->params = g_new0 (DviParams, 1);
322 dvi_document->params->dpi = MDVI_DPI;
323 dvi_document->params->vdpi = MDVI_VDPI;
324 dvi_document->params->mag = MDVI_MAGNIFICATION;
325 dvi_document->params->density = MDVI_DEFAULT_DENSITY;
326 dvi_document->params->gamma = MDVI_DEFAULT_GAMMA;
327 dvi_document->params->flags = MDVI_PARAM_ANTIALIASED;
328 dvi_document->params->hdrift = 0;
329 dvi_document->params->vdrift = 0;
330 dvi_document->params->hshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->dpi);
331 dvi_document->params->vshrink = MDVI_SHRINK_FROM_DPI(dvi_document->params->vdpi);
332 dvi_document->params->orientation = MDVI_ORIENT_TBLR;
334 dvi_document->spec = NULL;
336 dvi_document->params->bg = 0xffffffff;
337 dvi_document->params->fg = 0xff000000;
339 mdvi_init_kpathsea("evince", MDVI_MFMODE, MDVI_FALLBACK_FONT, MDVI_DPI);
341 mdvi_register_fonts ();
345 dvi_document_init (DviDocument *dvi_document)
347 dvi_document->context = NULL;
348 dvi_document_init_params (dvi_document);