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