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