]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
Update NEWS and require poppler 0.5.0.
[evince.git] / pixbuf / pixbuf-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2004, Anders Carlsson <andersca@gnome.org>
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19
20 #include "pixbuf-document.h"
21 #include "ev-document-thumbnails.h"
22
23 #include <libgnomevfs/gnome-vfs-uri.h>
24 #include <libgnomevfs/gnome-vfs-utils.h>
25 #include <libgnomevfs/gnome-vfs-ops.h>
26 #include <libgnomevfs/gnome-vfs-xfer.h>
27
28 struct _PixbufDocumentClass
29 {
30         GObjectClass parent_class;
31 };
32
33 struct _PixbufDocument
34 {
35         GObject parent_instance;
36
37         GdkPixbuf *pixbuf;
38         
39         gchar *uri;
40 };
41
42 typedef struct _PixbufDocumentClass PixbufDocumentClass;
43
44 static void pixbuf_document_document_iface_init (EvDocumentIface *iface);
45 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
46
47 G_DEFINE_TYPE_WITH_CODE (PixbufDocument, pixbuf_document, G_TYPE_OBJECT,
48                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
49                                                   pixbuf_document_document_iface_init);
50                          G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
51                                                 pixbuf_document_document_thumbnails_iface_init)                            
52                                    });
53
54 static gboolean
55 pixbuf_document_load (EvDocument  *document,
56                       const char  *uri,
57                       GError     **error)
58 {
59         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
60         
61         gchar *filename;
62         GdkPixbuf *pixbuf;
63
64         /* FIXME: We could actually load uris  */
65         filename = g_filename_from_uri (uri, NULL, error);
66         if (!filename)
67                 return FALSE;
68         
69         pixbuf = gdk_pixbuf_new_from_file (filename, error);
70
71         if (!pixbuf)
72                 return FALSE;
73
74         pixbuf_document->pixbuf = pixbuf;
75         g_free (pixbuf_document->uri);
76         pixbuf_document->uri = g_strdup (uri);
77         
78         return TRUE;
79 }
80
81 static gboolean
82 pixbuf_document_save (EvDocument  *document,
83                       const char  *uri,
84                       GError     **error)
85 {
86         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
87         GnomeVFSResult result;
88         GnomeVFSURI *source_uri;
89         GnomeVFSURI *target_uri;
90         
91         if (!pixbuf_document->uri)
92                 return FALSE;
93         
94         source_uri = gnome_vfs_uri_new (pixbuf_document->uri);
95         target_uri = gnome_vfs_uri_new (uri);
96
97         result = gnome_vfs_xfer_uri (source_uri, target_uri, 
98                                      GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
99                                      GNOME_VFS_XFER_ERROR_MODE_ABORT,
100                                      GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
101                                      NULL,
102                                      NULL);
103         gnome_vfs_uri_unref (target_uri);
104         gnome_vfs_uri_unref (source_uri);
105     
106         if (result != GNOME_VFS_OK)
107                 g_set_error (error,
108                              EV_DOCUMENT_ERROR,
109                              0,
110                              gnome_vfs_result_to_string (result));                      
111         return (result == GNOME_VFS_OK);
112 }
113
114 static int
115 pixbuf_document_get_n_pages (EvDocument  *document)
116 {
117         return 1;
118 }
119
120 static void
121 pixbuf_document_get_page_size (EvDocument   *document,
122                                int           page,
123                                double       *width,
124                                double       *height)
125 {
126         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
127
128         *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
129         *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
130 }
131
132 static GdkPixbuf*
133 pixbuf_document_render_pixbuf (EvDocument      *document,
134                                EvRenderContext *rc)
135 {
136         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
137         GdkPixbuf *scaled_pixbuf, *rotated_pixbuf;
138
139         scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
140                                                  gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale,
141                                                  gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale,
142                                                  GDK_INTERP_BILINEAR);
143
144         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
145         g_object_unref (scaled_pixbuf);
146
147         return rotated_pixbuf;
148 }
149
150 static void
151 pixbuf_document_finalize (GObject *object)
152 {
153         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
154
155         g_object_unref (pixbuf_document->pixbuf);
156         g_free (pixbuf_document->uri);
157         
158         G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
159 }
160
161 static void
162 pixbuf_document_class_init (PixbufDocumentClass *klass)
163 {
164         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165
166         gobject_class->finalize = pixbuf_document_finalize;
167 }
168
169 static gboolean
170 pixbuf_document_can_get_text (EvDocument *document)
171 {
172         return FALSE;
173 }
174
175 static EvDocumentInfo *
176 pixbuf_document_get_info (EvDocument *document)
177 {
178         EvDocumentInfo *info;
179
180         info = g_new0 (EvDocumentInfo, 1);
181         info->fields_mask = 0;
182
183         return info;
184 }
185
186 static void
187 pixbuf_document_document_iface_init (EvDocumentIface *iface)
188 {
189         iface->load = pixbuf_document_load;
190         iface->save = pixbuf_document_save;
191         iface->can_get_text = pixbuf_document_can_get_text;
192         iface->get_n_pages = pixbuf_document_get_n_pages;
193         iface->get_page_size = pixbuf_document_get_page_size;
194         iface->render_pixbuf = pixbuf_document_render_pixbuf;
195         iface->get_info = pixbuf_document_get_info;
196 }
197
198 static GdkPixbuf *
199 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
200                                           gint                    page,
201                                           gint                    rotation,
202                                           gint                    size,
203                                           gboolean                border)
204 {
205         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
206         GdkPixbuf *pixbuf, *rotated_pixbuf;
207         gdouble scale_factor;
208         gint height;
209         
210         scale_factor = (gdouble)size / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
211
212         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
213         
214         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, size, height,
215                                           GDK_INTERP_BILINEAR);
216
217         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation);
218         g_object_unref (pixbuf);
219
220         return rotated_pixbuf;
221 }
222
223 static void
224 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
225                                            gint                  page,
226                                            gint                  suggested_width,
227                                            gint                  *width,
228                                            gint                  *height)
229 {
230         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
231         gdouble page_ratio;
232
233         page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
234                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
235         *width = suggested_width;
236         *height = (gint) (suggested_width * page_ratio);
237 }
238
239 static void
240 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
241 {
242         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
243         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
244 }
245
246
247 static void
248 pixbuf_document_init (PixbufDocument *pixbuf_document)
249 {
250 }