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