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