]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
Move get_screen_dpi to libdocument
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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         gdk_draw_rectangle (drawable,
124                             highlight ?
125                                     widget->style->text_gc[widget->state] : widget->style->dark_gc[widget->state],
126                             TRUE,
127                             area->x,
128                             area->y,
129                             area->width,
130                             area->height);
131         gdk_draw_rectangle (drawable,
132                             widget->style->white_gc,
133                             TRUE,
134                             area->x + border->left,
135                             area->y + border->top,
136                             area->width - (border->left + border->right),
137                             area->height - (border->top + border->bottom));
138         gdk_draw_rectangle (drawable,
139                             widget->style->mid_gc[widget->state],
140                             TRUE,
141                             area->x,
142                             area->y + area->height - (border->bottom - border->top),
143                             border->bottom - border->top,
144                             border->bottom - border->top);
145         gdk_draw_rectangle (drawable,
146                             widget->style->mid_gc[widget->state],
147                             TRUE,
148                             area->x + area->width - (border->right - border->left),
149                             area->y,
150                             border->right - border->left,
151                             border->right - border->left);
152
153 }
154
155 cairo_surface_t *
156 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
157 {
158         cairo_surface_t *surface;
159         cairo_t         *cr;
160
161         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
162                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
163                                               gdk_pixbuf_get_width (pixbuf),
164                                               gdk_pixbuf_get_height (pixbuf));
165         cr = cairo_create (surface);
166         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
167         cairo_paint (cr);
168         cairo_destroy (cr);
169         
170         return surface;
171 }
172
173 GdkPixbuf *
174 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
175 {
176         GdkPixbuf       *pixbuf;
177         cairo_surface_t *image;
178         cairo_t         *cr;
179         gboolean         has_alpha;
180         gint             width, height;
181         cairo_format_t   surface_format;
182         gint             pixbuf_n_channels;
183         gint             pixbuf_rowstride;
184         guchar          *pixbuf_pixels;
185         gint             x, y;
186
187         width = cairo_image_surface_get_width (surface);
188         height = cairo_image_surface_get_height (surface);
189         
190         surface_format = cairo_image_surface_get_format (surface);
191         has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
192
193         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
194                                  TRUE, 8,
195                                  width, height);
196         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
197         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
198         pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
199
200         image = cairo_image_surface_create_for_data (pixbuf_pixels,
201                                                      surface_format,
202                                                      width, height,
203                                                      pixbuf_rowstride);
204         cr = cairo_create (image);
205         cairo_set_source_surface (cr, surface, 0, 0);
206
207         if (has_alpha)
208                 cairo_mask_surface (cr, surface, 0, 0);
209         else
210                 cairo_paint (cr);
211
212         cairo_destroy (cr);
213         cairo_surface_destroy (image);
214
215         for (y = 0; y < height; y++) {
216                 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
217
218                 for (x = 0; x < width; x++) {
219                         guchar tmp;
220                         
221 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
222                         tmp = p[0];
223                         p[0] = p[2];
224                         p[2] = tmp;
225                         p[3] = (has_alpha) ? p[3] : 0xff;
226 #else
227                         tmp = p[0];
228                         p[0] = p[1];
229                         p[1] = p[2];
230                         p[2] = p[3];
231                         p[3] = (has_alpha) ? tmp : 0xff;
232 #endif                  
233                         p += pixbuf_n_channels;
234                 }
235         }
236
237         return pixbuf;
238 }
239
240 cairo_surface_t *
241 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
242                                            gint             dest_width,
243                                            gint             dest_height,
244                                            gint             dest_rotation)
245 {
246         cairo_surface_t *new_surface;
247         cairo_t         *cr;
248         gint             width, height;
249         gint             new_width = dest_width;
250         gint             new_height = dest_height;
251
252         width = cairo_image_surface_get_width (surface);
253         height = cairo_image_surface_get_height (surface);
254         
255         if (dest_width == width &&
256             dest_height == height &&
257             dest_rotation == 0) {
258                 return cairo_surface_reference (surface);
259         }
260
261         if (dest_rotation == 90 || dest_rotation == 270) {
262                 new_width = dest_height;
263                 new_height = dest_width;
264         }
265
266         new_surface = cairo_surface_create_similar (surface,
267                                                     cairo_surface_get_content (surface),
268                                                     new_width, new_height);
269
270         cr = cairo_create (new_surface);
271         switch (dest_rotation) {
272                 case 90:
273                         cairo_translate (cr, new_width, 0);
274                         break;
275                 case 180:
276                         cairo_translate (cr, new_width, new_height);
277                         break;
278                 case 270:
279                         cairo_translate (cr, 0, new_height);
280                         break;
281                 default:
282                         cairo_translate (cr, 0, 0);
283         }
284         
285         if (dest_width != width || dest_height != height) {
286                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
287                 cairo_scale (cr,
288                              (gdouble)dest_width / width,
289                              (gdouble)dest_height / height);
290         }
291         
292         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
293         cairo_set_source_surface (cr, surface, 0, 0);
294         cairo_paint (cr);
295         cairo_destroy (cr);
296
297         return new_surface;
298 }
299
300 void
301 ev_document_misc_invert_surface (cairo_surface_t *surface) {
302 #if CAIRO_VERSION > CAIRO_VERSION_ENCODE(1, 9, 2)
303         cairo_t *cr;
304
305         cr = cairo_create (surface);
306
307         /* white + DIFFERENCE -> invert */
308         cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
309         cairo_set_source_rgb (cr, 1., 1., 1.);
310         cairo_paint(cr);
311         cairo_destroy (cr);
312 #else
313         guchar *data;
314         gint    rowstride;
315         gint    width, height;
316         gint    x, y;
317
318         data = cairo_image_surface_get_data (surface);
319         rowstride = cairo_image_surface_get_stride (surface);
320         width = cairo_image_surface_get_width (surface);
321         height = cairo_image_surface_get_height (surface);
322
323         for (y = 0; y < height; y++) {
324                 guchar *p = data + y * rowstride;
325
326                 for (x = 0; x < width; x++) {
327                         p[0] = 255 - p[0];
328                         p[1] = 255 - p[1];
329                         p[2] = 255 - p[2];
330                         p += 4;
331                 }
332         }
333
334         cairo_surface_mark_dirty (surface);
335 #endif
336 }
337
338 void
339 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
340 {
341         guchar *data, *p;
342         guint   width, height, x, y, rowstride, n_channels;
343
344         n_channels = gdk_pixbuf_get_n_channels (pixbuf);
345         g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
346         g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
347
348         /* First grab a pointer to the raw pixel data. */
349         data = gdk_pixbuf_get_pixels (pixbuf);
350
351         /* Find the number of bytes per row (could be padded). */
352         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
353
354         width = gdk_pixbuf_get_width (pixbuf);
355         height = gdk_pixbuf_get_height (pixbuf);
356         for (x = 0; x < width; x++) {
357                 for (y = 0; y < height; y++) {
358                         /* Calculate pixel's offset into the data array. */
359                         p = data + x * n_channels + y * rowstride;
360                         /* Change the RGB values*/
361                         p[0] = 255 - p[0];
362                         p[1] = 255 - p[1];
363                         p[2] = 255 - p[2];
364                 }
365         }
366 }
367
368 gdouble
369 ev_document_misc_get_screen_dpi (GdkScreen *screen)
370 {
371         gdouble dp, di;
372
373         /*diagonal in pixels*/
374         dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
375
376         /*diagonal in inches*/
377         di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;
378
379         return (dp / di);
380 }