]> www.fi.muni.cz Git - evince.git/blob - backend/pixbuf/pixbuf-document.c
ae7b4376cb27c116da0d8558bd6ddac1981a7667
[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-misc.h"
25 #include "ev-file-helpers.h"
26
27 struct _PixbufDocumentClass
28 {
29         EvDocumentClass parent_class;
30 };
31
32 struct _PixbufDocument
33 {
34         EvDocument parent_instance;
35
36         GdkPixbuf *pixbuf;
37         
38         gchar *uri;
39 };
40
41 typedef struct _PixbufDocumentClass PixbufDocumentClass;
42
43 EV_BACKEND_REGISTER (PixbufDocument, pixbuf_document)
44
45 static gboolean
46 pixbuf_document_load (EvDocument  *document,
47                       const char  *uri,
48                       GError     **error)
49 {
50         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
51         
52         gchar *filename;
53         GdkPixbuf *pixbuf;
54
55         /* FIXME: We could actually load uris  */
56         filename = g_filename_from_uri (uri, NULL, error);
57         if (!filename)
58                 return FALSE;
59         
60         pixbuf = gdk_pixbuf_new_from_file (filename, error);
61
62         if (!pixbuf)
63                 return FALSE;
64
65         pixbuf_document->pixbuf = pixbuf;
66         g_free (pixbuf_document->uri);
67         pixbuf_document->uri = g_strdup (uri);
68         
69         return TRUE;
70 }
71
72 static gboolean
73 pixbuf_document_save (EvDocument  *document,
74                       const char  *uri,
75                       GError     **error)
76 {
77         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
78
79         return ev_xfer_uri_simple (pixbuf_document->uri, uri, error); 
80 }
81
82 static int
83 pixbuf_document_get_n_pages (EvDocument  *document)
84 {
85         return 1;
86 }
87
88 static void
89 pixbuf_document_get_page_size (EvDocument   *document,
90                                EvPage       *page,
91                                double       *width,
92                                double       *height)
93 {
94         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
95
96         *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
97         *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
98 }
99
100 static cairo_surface_t *
101 pixbuf_document_render (EvDocument      *document,
102                         EvRenderContext *rc)
103 {
104         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
105         GdkPixbuf *scaled_pixbuf, *rotated_pixbuf;
106         cairo_surface_t *surface;
107
108         scaled_pixbuf = gdk_pixbuf_scale_simple (
109                 pixbuf_document->pixbuf,
110                 (gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale) + 0.5,
111                 (gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale) + 0.5,
112                 GDK_INTERP_BILINEAR);
113         
114         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
115         g_object_unref (scaled_pixbuf);
116
117         surface = ev_document_misc_surface_from_pixbuf (rotated_pixbuf);
118         g_object_unref (rotated_pixbuf);
119
120         return surface;
121 }
122
123 static GdkPixbuf *
124 pixbuf_document_get_thumbnail (EvDocument      *document,
125                                EvRenderContext *rc)
126 {
127         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
128         GdkPixbuf *pixbuf, *rotated_pixbuf;
129         gint width, height;
130         
131         width = (gint) (gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale);
132         height = (gint) (gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale);
133         
134         pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
135                                           width, height,
136                                           GDK_INTERP_BILINEAR);
137
138         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
139         g_object_unref (pixbuf);
140
141         return rotated_pixbuf;
142 }
143
144 static void
145 pixbuf_document_finalize (GObject *object)
146 {
147         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
148
149         g_object_unref (pixbuf_document->pixbuf);
150         g_free (pixbuf_document->uri);
151         
152         G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
153 }
154
155 static void
156 pixbuf_document_class_init (PixbufDocumentClass *klass)
157 {
158         GObjectClass    *gobject_class = G_OBJECT_CLASS (klass);
159         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
160
161         gobject_class->finalize = pixbuf_document_finalize;
162
163         ev_document_class->load = pixbuf_document_load;
164         ev_document_class->save = pixbuf_document_save;
165         ev_document_class->get_n_pages = pixbuf_document_get_n_pages;
166         ev_document_class->get_page_size = pixbuf_document_get_page_size;
167         ev_document_class->render = pixbuf_document_render;
168         ev_document_class->get_thumbnail = pixbuf_document_get_thumbnail;
169 }
170
171 static void
172 pixbuf_document_init (PixbufDocument *pixbuf_document)
173 {
174 }