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