]> www.fi.muni.cz Git - evince.git/blob - backend/pixbuf/pixbuf-document.c
83d2b8ae70d47bd6075c2bca0b910132a1e49ee3
[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
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 cairo_surface_t *
105 pixbuf_document_render (EvDocument      *document,
106                         EvRenderContext *rc)
107 {
108         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
109         GdkPixbuf *scaled_pixbuf, *rotated_pixbuf;
110         cairo_surface_t *surface;
111
112         scaled_pixbuf = gdk_pixbuf_scale_simple (
113                 pixbuf_document->pixbuf,
114                 (gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale) + 0.5,
115                 (gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale) + 0.5,
116                 GDK_INTERP_BILINEAR);
117         
118         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
119         g_object_unref (scaled_pixbuf);
120
121         surface = ev_document_misc_surface_from_pixbuf (rotated_pixbuf);
122         g_object_unref (rotated_pixbuf);
123
124         return surface;
125 }
126
127 static void
128 pixbuf_document_finalize (GObject *object)
129 {
130         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
131
132         g_object_unref (pixbuf_document->pixbuf);
133         g_free (pixbuf_document->uri);
134         
135         G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
136 }
137
138 static void
139 pixbuf_document_class_init (PixbufDocumentClass *klass)
140 {
141         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
142
143         gobject_class->finalize = pixbuf_document_finalize;
144 }
145
146 static gboolean
147 pixbuf_document_can_get_text (EvDocument *document)
148 {
149         return FALSE;
150 }
151
152 static EvDocumentInfo *
153 pixbuf_document_get_info (EvDocument *document)
154 {
155         EvDocumentInfo *info;
156
157         info = g_new0 (EvDocumentInfo, 1);
158         info->fields_mask = 0;
159
160         return info;
161 }
162
163 static void
164 pixbuf_document_document_iface_init (EvDocumentIface *iface)
165 {
166         iface->load = pixbuf_document_load;
167         iface->save = pixbuf_document_save;
168         iface->can_get_text = pixbuf_document_can_get_text;
169         iface->get_n_pages = pixbuf_document_get_n_pages;
170         iface->get_page_size = pixbuf_document_get_page_size;
171         iface->render = pixbuf_document_render;
172         iface->get_info = pixbuf_document_get_info;
173 }
174
175 static GdkPixbuf *
176 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
177                                           EvRenderContext      *rc,
178                                           gboolean              border)
179 {
180         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
181         GdkPixbuf *pixbuf, *rotated_pixbuf;
182         gint width, height;
183         
184         width = (gint) (gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale);
185         height = (gint) (gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale);
186         
187         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
188                                           width, height,
189                                           GDK_INTERP_BILINEAR);
190
191         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
192         g_object_unref (pixbuf);
193
194         return rotated_pixbuf;
195 }
196
197 static void
198 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
199                                            EvRenderContext      *rc, 
200                                            gint                 *width,
201                                            gint                 *height)
202 {
203         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
204         gint p_width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
205         gint p_height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
206
207         if (rc->rotation == 90 || rc->rotation == 270) {
208                 *width = (gint) (p_height * rc->scale);
209                 *height = (gint) (p_width * rc->scale);
210         } else {
211                 *width = (gint) (p_width * rc->scale);
212                 *height = (gint) (p_height * rc->scale);
213         }
214 }
215
216 static void
217 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
218 {
219         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
220         iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
221 }
222
223
224 static void
225 pixbuf_document_init (PixbufDocument *pixbuf_document)
226 {
227 }