]> www.fi.muni.cz Git - evince.git/blob - pixbuf/pixbuf-document.c
Add pixbuf backend.
[evince.git] / pixbuf / pixbuf-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pixbuf-document.h: Implementation of EvDocument for PIXBUF
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
22 struct _PixbufDocumentClass
23 {
24         GObjectClass parent_class;
25 };
26
27 struct _PixbufDocument
28 {
29         GObject parent_instance;
30
31         GdkPixbuf *pixbuf;
32         GdkDrawable *target;
33         gdouble scale;
34
35         gint x_offset, y_offset;
36 };
37
38 typedef struct _PixbufDocumentClass PixbufDocumentClass;
39
40 static void pixbuf_document_document_iface_init (EvDocumentIface *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
46 static GObjectClass *parent_class;
47
48 static gboolean
49 pixbuf_document_load (EvDocument  *document,
50                       const char  *uri,
51                       GError     **error)
52 {
53         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
54         
55         gchar *filename;
56         GdkPixbuf *pixbuf;
57
58         /* FIXME: We could actually load uris  */
59         filename = g_filename_from_uri (uri, NULL, error);
60         if (!filename)
61                 return FALSE;
62         
63         pixbuf = gdk_pixbuf_new_from_file (filename, error);
64
65         if (!pixbuf)
66                 return FALSE;
67
68         pixbuf_document->pixbuf = pixbuf;
69         
70         return TRUE;
71 }
72
73 static int
74 pixbuf_document_get_n_pages (EvDocument  *document)
75 {
76         return 1;
77 }
78
79 static void
80 pixbuf_document_set_page (EvDocument  *document,
81                           int          page)
82 {
83         /* Do nothing */
84 }
85
86 static int
87 pixbuf_document_get_page (EvDocument  *document)
88 {
89         return 1;
90 }
91
92 static void
93 pixbuf_document_set_target (EvDocument  *document,
94                             GdkDrawable *target)
95 {
96         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
97
98         pixbuf_document->target = target;
99 }
100
101 static void
102 pixbuf_document_set_scale (EvDocument  *document,
103                            double       scale)
104 {
105         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
106
107         pixbuf_document->scale = scale;
108 }
109
110 static void
111 pixbuf_document_set_page_offset (EvDocument  *document,
112                               int          x,
113                               int          y)
114 {
115         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
116
117         pixbuf_document->x_offset = x;
118         pixbuf_document->y_offset = y;
119 }
120
121 static void
122 pixbuf_document_get_page_size (EvDocument   *document,
123                                int          *width,
124                                int          *height)
125 {
126         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
127
128         *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
129         *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
130 }
131
132 static void
133 pixbuf_document_render (EvDocument  *document,
134                         int          clip_x,
135                         int          clip_y,
136                         int          clip_width,
137                         int          clip_height)
138 {
139         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
140
141         if (pixbuf_document->scale == 1.0) {
142                 gdk_draw_pixbuf (pixbuf_document->target, NULL, pixbuf_document->pixbuf,
143                                  clip_x, clip_y,
144                                  clip_x, clip_y,
145                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
146                                  0, 0);
147         }
148         else {
149                 GdkPixbuf *tmp_pixbuf;
150
151                 tmp_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf_document->pixbuf),
152                                              gdk_pixbuf_get_has_alpha (pixbuf_document->pixbuf),
153                                              gdk_pixbuf_get_bits_per_sample (pixbuf_document->pixbuf),
154                                              clip_width, clip_height);
155
156                 gdk_pixbuf_scale (pixbuf_document->pixbuf, tmp_pixbuf, 0, 0, clip_width, clip_height,
157                                   clip_x * pixbuf_document->scale,
158                                   clip_y * pixbuf_document->scale,
159                                   pixbuf_document->scale, pixbuf_document->scale,
160                                   GDK_INTERP_BILINEAR);
161                 
162                 gdk_draw_pixbuf (pixbuf_document->target, NULL, tmp_pixbuf,
163                                  0, 0,
164                                  clip_x + pixbuf_document->x_offset,
165                                  clip_y + pixbuf_document->y_offset,
166                                  clip_width, clip_height, GDK_RGB_DITHER_NORMAL,
167                                  0, 0);
168
169                 g_object_unref (tmp_pixbuf);
170         }
171 }
172
173 static void
174 pixbuf_document_begin_find (EvDocument   *document,
175                             const char   *search_string,
176                             gboolean      case_sensitive)
177 {
178         
179 }
180
181 static void
182 pixbuf_document_end_find (EvDocument   *document)
183 {
184 }
185
186 static void
187 pixbuf_document_finalize (GObject *object)
188 {
189         PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
190
191         g_object_unref (pixbuf_document->pixbuf);
192         
193         G_OBJECT_CLASS (parent_class)->finalize (object);
194 }
195
196 static void
197 pixbuf_document_class_init (PixbufDocumentClass *klass)
198 {
199         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
200
201         parent_class = g_type_class_peek_parent (klass);
202         
203         gobject_class->finalize = pixbuf_document_finalize;
204 }
205
206 static void
207 pixbuf_document_document_iface_init (EvDocumentIface *iface)
208 {
209         iface->load = pixbuf_document_load;
210         iface->get_n_pages = pixbuf_document_get_n_pages;
211         iface->set_page = pixbuf_document_set_page;
212         iface->get_page = pixbuf_document_get_page;
213         iface->set_scale = pixbuf_document_set_scale;
214         iface->set_target = pixbuf_document_set_target;
215         iface->set_page_offset = pixbuf_document_set_page_offset;
216         iface->get_page_size = pixbuf_document_get_page_size;
217         iface->render = pixbuf_document_render;
218         iface->begin_find = pixbuf_document_begin_find;
219         iface->end_find = pixbuf_document_end_find;
220 }
221
222 static void
223 pixbuf_document_init (PixbufDocument *pixbuf_document)
224 {
225         pixbuf_document->scale = 1.0;
226
227         pixbuf_document->x_offset = 10;
228         pixbuf_document->y_offset = 10;
229 }