]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
97f5271883ad516759a0bd1fa45d810f0e185c04
[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 struct _PixbufDocumentClass
24 {
25         GObjectClass parent_class;
26 };
27
28 struct _PixbufDocument
29 {
30         GObject parent_instance;
31
32         GdkPixbuf *pixbuf;
33         EvOrientation orientation;
34 };
35
36 typedef struct _PixbufDocumentClass PixbufDocumentClass;
37
38 static void pixbuf_document_document_iface_init (EvDocumentIface *iface);
39 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
40
41 G_DEFINE_TYPE_WITH_CODE (PixbufDocument, pixbuf_document, G_TYPE_OBJECT,
42                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
43                                                   pixbuf_document_document_iface_init);
44                          G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
45                                                 pixbuf_document_document_thumbnails_iface_init)                            
46                                    });
47
48 static gboolean
49 pixbuf_document_load (EvDocument  *document,
50                       const char  *uri,
51                       GError     **error)
52 {
53         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
54         
55         gchar *filename;
56         GdkPixbuf *pixbuf;
57
58         /* FIXME: We could actually load uris  */
59         filename = g_filename_from_uri (uri, NULL, error);
60         if (!filename)
61                 return FALSE;
62         
63         pixbuf = gdk_pixbuf_new_from_file (filename, error);
64
65         if (!pixbuf)
66                 return FALSE;
67
68         pixbuf_document->pixbuf = pixbuf;
69         
70         return TRUE;
71 }
72
73 static gboolean
74 pixbuf_document_save (EvDocument  *document,
75                       const char  *uri,
76                       GError     **error)
77 {
78         g_warning ("pixbuf_document_save not implemented"); /* FIXME */
79         return TRUE;
80 }
81
82 static int
83 pixbuf_document_get_n_pages (EvDocument  *document)
84 {
85         return 1;
86 }
87
88 static EvOrientation
89 pixbuf_document_get_orientation (EvDocument *document)
90 {
91         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
92
93         return pixbuf_document->orientation;
94 }
95
96 static void
97 pixbuf_document_set_orientation (EvDocument *document,
98                                  EvOrientation   orientation)
99 {
100         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
101
102         pixbuf_document->orientation = orientation;
103 }
104
105 static GdkPixbuf *
106 rotate_pixbuf (EvDocument *document, GdkPixbuf *pixbuf)
107 {
108         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
109
110         switch (pixbuf_document->orientation)
111         {
112                 case EV_ORIENTATION_LANDSCAPE:
113                         return gdk_pixbuf_rotate_simple (pixbuf, 90);
114                 case EV_ORIENTATION_UPSIDEDOWN:
115                         return gdk_pixbuf_rotate_simple (pixbuf, 180);
116                 case EV_ORIENTATION_SEASCAPE:
117                         return gdk_pixbuf_rotate_simple (pixbuf, 270);
118                 default:
119                         return g_object_ref (pixbuf);
120         }
121 }
122
123 static void
124 pixbuf_document_get_page_size (EvDocument   *document,
125                                int           page,
126                                double       *width,
127                                double       *height)
128 {
129         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
130
131         if (pixbuf_document->orientation == EV_ORIENTATION_PORTRAIT ||
132             pixbuf_document->orientation ==  EV_ORIENTATION_UPSIDEDOWN) {
133                 *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
134                 *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
135         } else {
136                 *width = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
137                 *height = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
138         }
139 }
140
141 static GdkPixbuf*
142 pixbuf_document_render_pixbuf (EvDocument  *document, int page, double scale)
143 {
144         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
145         GdkPixbuf *scaled_pixbuf, *rotated_pixbuf;
146
147         scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
148                                                  gdk_pixbuf_get_width (pixbuf_document->pixbuf) * scale,
149                                                  gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale,
150                                                  GDK_INTERP_BILINEAR);
151
152         rotated_pixbuf = rotate_pixbuf (document, scaled_pixbuf);
153         g_object_unref (scaled_pixbuf);
154
155         return rotated_pixbuf;
156 }
157
158 static void
159 pixbuf_document_finalize (GObject *object)
160 {
161         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
162
163         g_object_unref (pixbuf_document->pixbuf);
164         
165         G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
166 }
167
168 static void
169 pixbuf_document_class_init (PixbufDocumentClass *klass)
170 {
171         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
172
173         gobject_class->finalize = pixbuf_document_finalize;
174 }
175
176 static gboolean
177 pixbuf_document_can_get_text (EvDocument *document)
178 {
179         return FALSE;
180 }
181
182 static EvDocumentInfo *
183 pixbuf_document_get_info (EvDocument *document)
184 {
185         EvDocumentInfo *info;
186
187         info = g_new0 (EvDocumentInfo, 1);
188         info->fields_mask = 0;
189
190         return info;
191 }
192
193 static void
194 pixbuf_document_document_iface_init (EvDocumentIface *iface)
195 {
196         iface->load = pixbuf_document_load;
197         iface->save = pixbuf_document_save;
198         iface->can_get_text = pixbuf_document_can_get_text;
199         iface->get_n_pages = pixbuf_document_get_n_pages;
200         iface->get_page_size = pixbuf_document_get_page_size;
201         iface->render_pixbuf = pixbuf_document_render_pixbuf;
202         iface->get_info = pixbuf_document_get_info;
203         iface->get_orientation = pixbuf_document_get_orientation;
204         iface->set_orientation = pixbuf_document_set_orientation;
205 }
206
207 static GdkPixbuf *
208 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
209                                           gint                    page,
210                                           gint                    size,
211                                           gboolean                border)
212 {
213         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
214         GdkPixbuf *pixbuf;
215         gdouble scale_factor;
216         gint height;
217         
218         scale_factor = (gdouble)size / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
219
220         height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
221         
222         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, size, height,
223                                           GDK_INTERP_BILINEAR);
224         
225         return pixbuf;
226 }
227
228 static void
229 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
230                                            gint                  page,
231                                            gint                  suggested_width,
232                                            gint                  *width,
233                                            gint                  *height)
234 {
235         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
236         gdouble page_ratio;
237
238         page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
239                      gdk_pixbuf_get_width (pixbuf_document->pixbuf);
240         *width = suggested_width;
241         *height = (gint) (suggested_width * page_ratio);
242 }
243
244 static void
245 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
246 {
247         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
248         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
249 }
250
251
252 static void
253 pixbuf_document_init (PixbufDocument *pixbuf_document)
254 {
255 }