]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
Update FSF address everywhere.
[evince.git] / libdocument / ev-document-misc.c
1 /*
2  *  Copyright (C) 2009 Juanjo MarĂ­n <juanj.marin@juntadeandalucia.es>
3  *  Copyright (c) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
4  *  Copyright (C) 2000-2003 Marco Pesenti Gritti
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <math.h>
25
26 #include <gtk/gtk.h>
27
28 #include "ev-document-misc.h"
29
30 /* Returns a new GdkPixbuf that is suitable for placing in the thumbnail view.
31  * It is four pixels wider and taller than the source.  If source_pixbuf is not
32  * NULL, then it will fill the return pixbuf with the contents of
33  * source_pixbuf.
34  */
35 GdkPixbuf *
36 ev_document_misc_get_thumbnail_frame (int        width,
37                                       int        height,
38                                       GdkPixbuf *source_pixbuf)
39 {
40         GdkPixbuf *retval;
41         guchar *data;
42         gint rowstride;
43         int i;
44         int width_r, height_r;
45
46         if (source_pixbuf)
47                 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
48
49         if (source_pixbuf) {
50                 width_r = gdk_pixbuf_get_width (source_pixbuf);
51                 height_r = gdk_pixbuf_get_height (source_pixbuf);
52         } else {
53                 width_r = width;
54                 height_r = height;
55         }
56
57         /* make sure no one is passing us garbage */
58         g_assert (width_r >= 0 && height_r >= 0);
59
60         retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
61                                  TRUE, 8,
62                                  width_r + 4,
63                                  height_r + 4);
64
65         /* make it black and fill in the middle */
66         data = gdk_pixbuf_get_pixels (retval);
67         rowstride = gdk_pixbuf_get_rowstride (retval);
68
69         gdk_pixbuf_fill (retval, 0x000000ff);
70         for (i = 1; i < height_r + 1; i++)
71                 memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
72
73         /* copy the source pixbuf */
74         if (source_pixbuf)
75                 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
76                                       width_r,
77                                       height_r,
78                                       retval,
79                                       1, 1);
80         /* Add the corner */
81         data [(width_r + 2) * 4 + 3] = 0;
82         data [(width_r + 3) * 4 + 3] = 0;
83         data [(width_r + 2) * 4 + (rowstride * 1) + 3] = 0;
84         data [(width_r + 3) * 4 + (rowstride * 1) + 3] = 0;
85
86         data [(height_r + 2) * rowstride + 3] = 0;
87         data [(height_r + 3) * rowstride + 3] = 0;
88         data [(height_r + 2) * rowstride + 4 + 3] = 0;
89         data [(height_r + 3) * rowstride + 4 + 3] = 0;
90
91         return retval;
92 }
93
94 void
95 ev_document_misc_get_page_border_size (gint       page_width,
96                                        gint       page_height,
97                                        GtkBorder *border)
98 {
99         g_assert (border);
100
101         border->left = 1;
102         border->top = 1;
103         if (page_width < 100) {
104                 border->right = 2;
105                 border->bottom = 2;
106         } else if (page_width < 500) {
107                 border->right = 3;
108                 border->bottom = 3;
109         } else {
110                 border->right = 4;
111                 border->bottom = 4;
112         }
113 }
114
115
116 void
117 ev_document_misc_paint_one_page (GdkDrawable  *drawable,
118                                  GtkWidget    *widget,
119                                  GdkRectangle *area,
120                                  GtkBorder    *border,
121                                  gboolean highlight)
122 {
123         GtkStyle    *style = gtk_widget_get_style (widget);
124         GtkStateType state = gtk_widget_get_state (widget);
125
126         gdk_draw_rectangle (drawable,
127                             highlight ? style->text_gc[state] : style->dark_gc[state],
128                             TRUE,
129                             area->x,
130                             area->y,
131                             area->width,
132                             area->height);
133         gdk_draw_rectangle (drawable,
134                             style->white_gc,
135                             TRUE,
136                             area->x + border->left,
137                             area->y + border->top,
138                             area->width - (border->left + border->right),
139                             area->height - (border->top + border->bottom));
140         gdk_draw_rectangle (drawable,
141                             style->mid_gc[state],
142                             TRUE,
143                             area->x,
144                             area->y + area->height - (border->bottom - border->top),
145                             border->bottom - border->top,
146                             border->bottom - border->top);
147         gdk_draw_rectangle (drawable,
148                             style->mid_gc[state],
149                             TRUE,
150                             area->x + area->width - (border->right - border->left),
151                             area->y,
152                             border->right - border->left,
153                             border->right - border->left);
154
155 }
156
157 cairo_surface_t *
158 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
159 {
160         cairo_surface_t *surface;
161         cairo_t         *cr;
162
163         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
164                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
165                                               gdk_pixbuf_get_width (pixbuf),
166                                               gdk_pixbuf_get_height (pixbuf));
167         cr = cairo_create (surface);
168         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
169         cairo_paint (cr);
170         cairo_destroy (cr);
171         
172         return surface;
173 }
174
175 GdkPixbuf *
176 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
177 {
178         GdkPixbuf       *pixbuf;
179         cairo_surface_t *image;
180         cairo_t         *cr;
181         gboolean         has_alpha;
182         gint             width, height;
183         cairo_format_t   surface_format;
184         gint             pixbuf_n_channels;
185         gint             pixbuf_rowstride;
186         guchar          *pixbuf_pixels;
187         gint             x, y;
188
189         width = cairo_image_surface_get_width (surface);
190         height = cairo_image_surface_get_height (surface);
191         
192         surface_format = cairo_image_surface_get_format (surface);
193         has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
194
195         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
196                                  TRUE, 8,
197                                  width, height);
198         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
199         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
200         pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
201
202         image = cairo_image_surface_create_for_data (pixbuf_pixels,
203                                                      surface_format,
204                                                      width, height,
205                                                      pixbuf_rowstride);
206         cr = cairo_create (image);
207         cairo_set_source_surface (cr, surface, 0, 0);
208
209         if (has_alpha)
210                 cairo_mask_surface (cr, surface, 0, 0);
211         else
212                 cairo_paint (cr);
213
214         cairo_destroy (cr);
215         cairo_surface_destroy (image);
216
217         for (y = 0; y < height; y++) {
218                 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
219
220                 for (x = 0; x < width; x++) {
221                         guchar tmp;
222                         
223 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
224                         tmp = p[0];
225                         p[0] = p[2];
226                         p[2] = tmp;
227                         p[3] = (has_alpha) ? p[3] : 0xff;
228 #else
229                         tmp = p[0];
230                         p[0] = p[1];
231                         p[1] = p[2];
232                         p[2] = p[3];
233                         p[3] = (has_alpha) ? tmp : 0xff;
234 #endif                  
235                         p += pixbuf_n_channels;
236                 }
237         }
238
239         return pixbuf;
240 }
241
242 cairo_surface_t *
243 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
244                                            gint             dest_width,
245                                            gint             dest_height,
246                                            gint             dest_rotation)
247 {
248         cairo_surface_t *new_surface;
249         cairo_t         *cr;
250         gint             width, height;
251         gint             new_width = dest_width;
252         gint             new_height = dest_height;
253
254         width = cairo_image_surface_get_width (surface);
255         height = cairo_image_surface_get_height (surface);
256         
257         if (dest_width == width &&
258             dest_height == height &&
259             dest_rotation == 0) {
260                 return cairo_surface_reference (surface);
261         }
262
263         if (dest_rotation == 90 || dest_rotation == 270) {
264                 new_width = dest_height;
265                 new_height = dest_width;
266         }
267
268         new_surface = cairo_surface_create_similar (surface,
269                                                     cairo_surface_get_content (surface),
270                                                     new_width, new_height);
271
272         cr = cairo_create (new_surface);
273         switch (dest_rotation) {
274                 case 90:
275                         cairo_translate (cr, new_width, 0);
276                         break;
277                 case 180:
278                         cairo_translate (cr, new_width, new_height);
279                         break;
280                 case 270:
281                         cairo_translate (cr, 0, new_height);
282                         break;
283                 default:
284                         cairo_translate (cr, 0, 0);
285         }
286         
287         if (dest_width != width || dest_height != height) {
288                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
289                 cairo_scale (cr,
290                              (gdouble)dest_width / width,
291                              (gdouble)dest_height / height);
292         }
293         
294         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
295         cairo_set_source_surface (cr, surface, 0, 0);
296         cairo_paint (cr);
297         cairo_destroy (cr);
298
299         return new_surface;
300 }
301
302 void
303 ev_document_misc_invert_surface (cairo_surface_t *surface) {
304 #if CAIRO_VERSION > CAIRO_VERSION_ENCODE(1, 9, 2)
305         cairo_t *cr;
306
307         cr = cairo_create (surface);
308
309         /* white + DIFFERENCE -> invert */
310         cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
311         cairo_set_source_rgb (cr, 1., 1., 1.);
312         cairo_paint(cr);
313         cairo_destroy (cr);
314 #else
315         guchar *data;
316         gint    rowstride;
317         gint    width, height;
318         gint    x, y;
319
320         data = cairo_image_surface_get_data (surface);
321         rowstride = cairo_image_surface_get_stride (surface);
322         width = cairo_image_surface_get_width (surface);
323         height = cairo_image_surface_get_height (surface);
324
325         for (y = 0; y < height; y++) {
326                 guchar *p = data + y * rowstride;
327
328                 for (x = 0; x < width; x++) {
329                         p[0] = 255 - p[0];
330                         p[1] = 255 - p[1];
331                         p[2] = 255 - p[2];
332                         p += 4;
333                 }
334         }
335
336         cairo_surface_mark_dirty (surface);
337 #endif
338 }
339
340 void
341 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
342 {
343         guchar *data, *p;
344         guint   width, height, x, y, rowstride, n_channels;
345
346         n_channels = gdk_pixbuf_get_n_channels (pixbuf);
347         g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
348         g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
349
350         /* First grab a pointer to the raw pixel data. */
351         data = gdk_pixbuf_get_pixels (pixbuf);
352
353         /* Find the number of bytes per row (could be padded). */
354         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
355
356         width = gdk_pixbuf_get_width (pixbuf);
357         height = gdk_pixbuf_get_height (pixbuf);
358         for (x = 0; x < width; x++) {
359                 for (y = 0; y < height; y++) {
360                         /* Calculate pixel's offset into the data array. */
361                         p = data + x * n_channels + y * rowstride;
362                         /* Change the RGB values*/
363                         p[0] = 255 - p[0];
364                         p[1] = 255 - p[1];
365                         p[2] = 255 - p[2];
366                 }
367         }
368 }
369
370 gdouble
371 ev_document_misc_get_screen_dpi (GdkScreen *screen)
372 {
373         gdouble dp, di;
374
375         /*diagonal in pixels*/
376         dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
377
378         /*diagonal in inches*/
379         di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;
380
381         return (dp / di);
382 }