]> www.fi.muni.cz Git - evince.git/blob - backend/pixbuf/pixbuf-document.c
Remove get_dimensions() from EvDocumentThumbnails interface
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <config.h>
21 #include <glib/gi18n-lib.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         EvDocumentClass parent_class;
31 };
32
33 struct _PixbufDocument
34 {
35         EvDocument parent_instance;
36
37         GdkPixbuf *pixbuf;
38         
39         gchar *uri;
40 };
41
42 typedef struct _PixbufDocumentClass PixbufDocumentClass;
43
44 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface);
45
46 EV_BACKEND_REGISTER_WITH_CODE (PixbufDocument, pixbuf_document,
47                    {
48                          EV_BACKEND_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                                EvPage       *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         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
146
147         gobject_class->finalize = pixbuf_document_finalize;
148
149         ev_document_class->load = pixbuf_document_load;
150         ev_document_class->save = pixbuf_document_save;
151         ev_document_class->get_n_pages = pixbuf_document_get_n_pages;
152         ev_document_class->get_page_size = pixbuf_document_get_page_size;
153         ev_document_class->render = pixbuf_document_render;
154 }
155
156 static GdkPixbuf *
157 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
158                                           EvRenderContext      *rc,
159                                           gboolean              border)
160 {
161         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
162         GdkPixbuf *pixbuf, *rotated_pixbuf;
163         gint width, height;
164         
165         width = (gint) (gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale);
166         height = (gint) (gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale);
167         
168         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
169                                           width, height,
170                                           GDK_INTERP_BILINEAR);
171
172         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
173         g_object_unref (pixbuf);
174
175         return rotated_pixbuf;
176 }
177
178 static void
179 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
180 {
181         iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
182 }
183
184
185 static void
186 pixbuf_document_init (PixbufDocument *pixbuf_document)
187 {
188 }