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