]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
[build] Get rid of shave with AM_SILENT_RULES automake option
[evince.git] / libdocument / ev-document-misc.c
1 /*
2  *  Copyright (c) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
3  *  Copyright (C) 2000-2003 Marco Pesenti Gritti
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
22 #include <string.h>
23
24 #include <gtk/gtk.h>
25
26 #include "ev-document-misc.h"
27
28 /* Returns a new GdkPixbuf that is suitable for placing in the thumbnail view.
29  * It is four pixels wider and taller than the source.  If source_pixbuf is not
30  * NULL, then it will fill the return pixbuf with the contents of
31  * source_pixbuf.
32  */
33 GdkPixbuf *
34 ev_document_misc_get_thumbnail_frame (int        width,
35                                       int        height,
36                                       GdkPixbuf *source_pixbuf)
37 {
38         GdkPixbuf *retval;
39         guchar *data;
40         gint rowstride;
41         int i;
42         int width_r, height_r;
43
44         if (source_pixbuf)
45                 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
46
47         if (source_pixbuf) {
48                 width_r = gdk_pixbuf_get_width (source_pixbuf);
49                 height_r = gdk_pixbuf_get_height (source_pixbuf);
50         } else {
51                 width_r = width;
52                 height_r = height;
53         }
54
55         /* make sure no one is passing us garbage */
56         g_assert (width_r >= 0 && height_r >= 0);
57
58         retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
59                                  TRUE, 8,
60                                  width_r + 4,
61                                  height_r + 4);
62
63         /* make it black and fill in the middle */
64         data = gdk_pixbuf_get_pixels (retval);
65         rowstride = gdk_pixbuf_get_rowstride (retval);
66
67         gdk_pixbuf_fill (retval, 0x000000ff);
68         for (i = 1; i < height_r + 1; i++)
69                 memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
70
71         /* copy the source pixbuf */
72         if (source_pixbuf)
73                 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
74                                       width_r,
75                                       height_r,
76                                       retval,
77                                       1, 1);
78         /* Add the corner */
79         data [(width_r + 2) * 4 + 3] = 0;
80         data [(width_r + 3) * 4 + 3] = 0;
81         data [(width_r + 2) * 4 + (rowstride * 1) + 3] = 0;
82         data [(width_r + 3) * 4 + (rowstride * 1) + 3] = 0;
83
84         data [(height_r + 2) * rowstride + 3] = 0;
85         data [(height_r + 3) * rowstride + 3] = 0;
86         data [(height_r + 2) * rowstride + 4 + 3] = 0;
87         data [(height_r + 3) * rowstride + 4 + 3] = 0;
88
89         return retval;
90 }
91
92 void
93 ev_document_misc_get_page_border_size (gint       page_width,
94                                        gint       page_height,
95                                        GtkBorder *border)
96 {
97         g_assert (border);
98
99         border->left = 1;
100         border->top = 1;
101         if (page_width < 100) {
102                 border->right = 2;
103                 border->bottom = 2;
104         } else if (page_width < 500) {
105                 border->right = 3;
106                 border->bottom = 3;
107         } else {
108                 border->right = 4;
109                 border->bottom = 4;
110         }
111 }
112
113
114 void
115 ev_document_misc_paint_one_page (GdkDrawable  *drawable,
116                                  GtkWidget    *widget,
117                                  GdkRectangle *area,
118                                  GtkBorder    *border,
119                                  gboolean highlight)
120 {
121         gdk_draw_rectangle (drawable,
122                             highlight ?
123                                     widget->style->text_gc[widget->state] : widget->style->dark_gc[widget->state],
124                             TRUE,
125                             area->x,
126                             area->y,
127                             area->width,
128                             area->height);
129         gdk_draw_rectangle (drawable,
130                             widget->style->white_gc,
131                             TRUE,
132                             area->x + border->left,
133                             area->y + border->top,
134                             area->width - (border->left + border->right),
135                             area->height - (border->top + border->bottom));
136         gdk_draw_rectangle (drawable,
137                             widget->style->mid_gc[widget->state],
138                             TRUE,
139                             area->x,
140                             area->y + area->height - (border->bottom - border->top),
141                             border->bottom - border->top,
142                             border->bottom - border->top);
143         gdk_draw_rectangle (drawable,
144                             widget->style->mid_gc[widget->state],
145                             TRUE,
146                             area->x + area->width - (border->right - border->left),
147                             area->y,
148                             border->right - border->left,
149                             border->right - border->left);
150
151 }
152
153 cairo_surface_t *
154 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
155 {
156         cairo_surface_t *surface;
157         cairo_t         *cr;
158
159         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
160                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
161                                               gdk_pixbuf_get_width (pixbuf),
162                                               gdk_pixbuf_get_height (pixbuf));
163         cr = cairo_create (surface);
164         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
165         cairo_paint (cr);
166         cairo_destroy (cr);
167         
168         return surface;
169 }
170
171 GdkPixbuf *
172 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
173 {
174         GdkPixbuf       *pixbuf;
175         cairo_surface_t *image;
176         cairo_t         *cr;
177         gboolean         has_alpha;
178         gint             width, height;
179         cairo_format_t   surface_format;
180         gint             pixbuf_n_channels;
181         gint             pixbuf_rowstride;
182         guchar          *pixbuf_pixels;
183         gint             x, y;
184
185         width = cairo_image_surface_get_width (surface);
186         height = cairo_image_surface_get_height (surface);
187         
188         surface_format = cairo_image_surface_get_format (surface);
189         has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
190
191         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
192                                  TRUE, 8,
193                                  width, height);
194         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
195         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
196         pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
197
198         image = cairo_image_surface_create_for_data (pixbuf_pixels,
199                                                      surface_format,
200                                                      width, height,
201                                                      pixbuf_rowstride);
202         cr = cairo_create (image);
203         cairo_set_source_surface (cr, surface, 0, 0);
204
205         if (has_alpha)
206                 cairo_mask_surface (cr, surface, 0, 0);
207         else
208                 cairo_paint (cr);
209
210         cairo_destroy (cr);
211         cairo_surface_destroy (image);
212
213         for (y = 0; y < height; y++) {
214                 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
215
216                 for (x = 0; x < width; x++) {
217                         guchar tmp;
218                         
219 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
220                         tmp = p[0];
221                         p[0] = p[2];
222                         p[2] = tmp;
223                         p[3] = (has_alpha) ? p[3] : 0xff;
224 #else
225                         tmp = p[0];
226                         p[0] = p[1];
227                         p[1] = p[2];
228                         p[2] = p[3];
229                         p[3] = (has_alpha) ? tmp : 0xff;
230 #endif                  
231                         p += pixbuf_n_channels;
232                 }
233         }
234
235         return pixbuf;
236 }
237
238 cairo_surface_t *
239 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
240                                            gint             dest_width,
241                                            gint             dest_height,
242                                            gint             dest_rotation)
243 {
244         cairo_surface_t *new_surface;
245         cairo_t         *cr;
246         gint             width, height;
247         gint             new_width = dest_width;
248         gint             new_height = dest_height;
249
250         width = cairo_image_surface_get_width (surface);
251         height = cairo_image_surface_get_height (surface);
252         
253         if (dest_width == width &&
254             dest_height == height &&
255             dest_rotation == 0) {
256                 return cairo_surface_reference (surface);
257         }
258
259         if (dest_rotation == 90 || dest_rotation == 270) {
260                 new_width = dest_height;
261                 new_height = dest_width;
262         }
263
264         new_surface = cairo_surface_create_similar (surface,
265                                                     cairo_surface_get_content (surface),
266                                                     new_width, new_height);
267
268         cr = cairo_create (new_surface);
269         switch (dest_rotation) {
270                 case 90:
271                         cairo_translate (cr, new_width, 0);
272                         break;
273                 case 180:
274                         cairo_translate (cr, new_width, new_height);
275                         break;
276                 case 270:
277                         cairo_translate (cr, 0, new_height);
278                         break;
279                 default:
280                         cairo_translate (cr, 0, 0);
281         }
282         
283         if (dest_width != width || dest_height != height) {
284                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
285                 cairo_scale (cr,
286                              (gdouble)dest_width / width,
287                              (gdouble)dest_height / height);
288         }
289         
290         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
291         cairo_set_source_surface (cr, surface, 0, 0);
292         cairo_paint (cr);
293         cairo_destroy (cr);
294
295         return new_surface;
296 }
297