]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
libdocument: Add more sanity checks to ev-document-misc.
[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 static GdkPixbuf *
36 create_thumbnail_frame (int        width,
37                         int        height,
38                         GdkPixbuf *source_pixbuf,
39                         gboolean   fill_bg)
40 {
41         GdkPixbuf *retval;
42         guchar *data;
43         gint rowstride;
44         int i;
45         int width_r, height_r;
46
47         if (source_pixbuf)
48                 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
49
50         if (source_pixbuf) {
51                 width_r = gdk_pixbuf_get_width (source_pixbuf);
52                 height_r = gdk_pixbuf_get_height (source_pixbuf);
53         } else {
54                 width_r = width;
55                 height_r = height;
56         }
57
58         /* make sure no one is passing us garbage */
59         g_return_val_if_fail (width_r >= 0 && height_r >= 0, NULL);
60
61         retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
62                                  TRUE, 8,
63                                  width_r + 4,
64                                  height_r + 4);
65
66         /* make it black and fill in the middle */
67         data = gdk_pixbuf_get_pixels (retval);
68         rowstride = gdk_pixbuf_get_rowstride (retval);
69
70         gdk_pixbuf_fill (retval, 0x000000ff);
71         if (fill_bg) {
72                 for (i = 1; i < height_r + 1; i++)
73                         memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
74         }
75
76         /* copy the source pixbuf */
77         if (source_pixbuf)
78                 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
79                                       width_r,
80                                       height_r,
81                                       retval,
82                                       1, 1);
83         /* Add the corner */
84         data [(width_r + 2) * 4 + 3] = 0;
85         data [(width_r + 3) * 4 + 3] = 0;
86         data [(width_r + 2) * 4 + (rowstride * 1) + 3] = 0;
87         data [(width_r + 3) * 4 + (rowstride * 1) + 3] = 0;
88
89         data [(height_r + 2) * rowstride + 3] = 0;
90         data [(height_r + 3) * rowstride + 3] = 0;
91         data [(height_r + 2) * rowstride + 4 + 3] = 0;
92         data [(height_r + 3) * rowstride + 4 + 3] = 0;
93
94         return retval;
95 }
96
97 GdkPixbuf *
98 ev_document_misc_get_thumbnail_frame (int        width,
99                                       int        height,
100                                       GdkPixbuf *source_pixbuf)
101 {
102         return create_thumbnail_frame (width, height, source_pixbuf, TRUE);
103 }
104
105 GdkPixbuf *
106 ev_document_misc_get_loading_thumbnail (int      width,
107                                         int      height,
108                                         gboolean inverted_colors)
109 {
110         return create_thumbnail_frame (width, height, NULL, !inverted_colors);
111 }
112
113 void
114 ev_document_misc_get_page_border_size (gint       page_width,
115                                        gint       page_height,
116                                        GtkBorder *border)
117 {
118         g_assert (border);
119
120         border->left = 1;
121         border->top = 1;
122         if (page_width < 100) {
123                 border->right = 2;
124                 border->bottom = 2;
125         } else if (page_width < 500) {
126                 border->right = 3;
127                 border->bottom = 3;
128         } else {
129                 border->right = 4;
130                 border->bottom = 4;
131         }
132 }
133
134
135 void
136 ev_document_misc_paint_one_page (cairo_t      *cr,
137                                  GtkWidget    *widget,
138                                  GdkRectangle *area,
139                                  GtkBorder    *border,
140                                  gboolean      highlight,
141                                  gboolean      inverted_colors)
142 {
143         GtkStyle    *style = gtk_widget_get_style (widget);
144         GtkStateType state = gtk_widget_get_state (widget);
145
146         gdk_cairo_set_source_color (cr, highlight ? &style->text[state] : &style->dark[state]);
147         gdk_cairo_rectangle (cr, area);
148         cairo_fill (cr);
149
150         if (inverted_colors)
151                 cairo_set_source_rgb (cr, 0, 0, 0);
152         else
153                 cairo_set_source_rgb (cr, 1, 1, 1);
154         cairo_rectangle (cr,
155                          area->x + border->left,
156                          area->y + border->top,
157                          area->width - (border->left + border->right),
158                          area->height - (border->top + border->bottom));
159         cairo_fill (cr);
160
161         gdk_cairo_set_source_color (cr, &style->mid[state]);
162         cairo_rectangle (cr,
163                          area->x,
164                          area->y + area->height - (border->bottom - border->top),
165                          border->bottom - border->top,
166                          border->bottom - border->top);
167         cairo_fill (cr);
168
169         cairo_rectangle (cr,
170                          area->x + area->width - (border->right - border->left),
171                          area->y,
172                          border->right - border->left,
173                          border->right - border->left);
174         cairo_fill (cr);
175 }
176
177 cairo_surface_t *
178 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
179 {
180         cairo_surface_t *surface;
181         cairo_t         *cr;
182
183         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
184
185         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
186                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
187                                               gdk_pixbuf_get_width (pixbuf),
188                                               gdk_pixbuf_get_height (pixbuf));
189         cr = cairo_create (surface);
190         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
191         cairo_paint (cr);
192         cairo_destroy (cr);
193         
194         return surface;
195 }
196
197 GdkPixbuf *
198 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
199 {
200         g_return_val_if_fail (surface, NULL);   
201
202         return gdk_pixbuf_get_from_surface (surface,
203                                             0, 0,
204                                             cairo_image_surface_get_width (surface),
205                                             cairo_image_surface_get_height (surface));
206 }
207
208 cairo_surface_t *
209 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
210                                            gint             dest_width,
211                                            gint             dest_height,
212                                            gint             dest_rotation)
213 {
214         cairo_surface_t *new_surface;
215         cairo_t         *cr;
216         gint             width, height;
217         gint             new_width = dest_width;
218         gint             new_height = dest_height;
219
220         width = cairo_image_surface_get_width (surface);
221         height = cairo_image_surface_get_height (surface);
222         
223         if (dest_width == width &&
224             dest_height == height &&
225             dest_rotation == 0) {
226                 return cairo_surface_reference (surface);
227         }
228
229         if (dest_rotation == 90 || dest_rotation == 270) {
230                 new_width = dest_height;
231                 new_height = dest_width;
232         }
233
234         new_surface = cairo_surface_create_similar (surface,
235                                                     cairo_surface_get_content (surface),
236                                                     new_width, new_height);
237
238         cr = cairo_create (new_surface);
239         switch (dest_rotation) {
240                 case 90:
241                         cairo_translate (cr, new_width, 0);
242                         break;
243                 case 180:
244                         cairo_translate (cr, new_width, new_height);
245                         break;
246                 case 270:
247                         cairo_translate (cr, 0, new_height);
248                         break;
249                 default:
250                         cairo_translate (cr, 0, 0);
251         }
252         
253         if (dest_width != width || dest_height != height) {
254                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
255                 cairo_scale (cr,
256                              (gdouble)dest_width / width,
257                              (gdouble)dest_height / height);
258         }
259         
260         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
261         cairo_set_source_surface (cr, surface, 0, 0);
262         cairo_paint (cr);
263         cairo_destroy (cr);
264
265         return new_surface;
266 }
267
268 void
269 ev_document_misc_invert_surface (cairo_surface_t *surface) {
270         cairo_t *cr;
271
272         cr = cairo_create (surface);
273
274         /* white + DIFFERENCE -> invert */
275         cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
276         cairo_set_source_rgb (cr, 1., 1., 1.);
277         cairo_paint(cr);
278         cairo_destroy (cr);
279 }
280
281 void
282 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
283 {
284         guchar *data, *p;
285         guint   width, height, x, y, rowstride, n_channels;
286
287         n_channels = gdk_pixbuf_get_n_channels (pixbuf);
288         g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
289         g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
290
291         /* First grab a pointer to the raw pixel data. */
292         data = gdk_pixbuf_get_pixels (pixbuf);
293
294         /* Find the number of bytes per row (could be padded). */
295         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
296
297         width = gdk_pixbuf_get_width (pixbuf);
298         height = gdk_pixbuf_get_height (pixbuf);
299         for (x = 0; x < width; x++) {
300                 for (y = 0; y < height; y++) {
301                         /* Calculate pixel's offset into the data array. */
302                         p = data + x * n_channels + y * rowstride;
303                         /* Change the RGB values*/
304                         p[0] = 255 - p[0];
305                         p[1] = 255 - p[1];
306                         p[2] = 255 - p[2];
307                 }
308         }
309 }
310
311 gdouble
312 ev_document_misc_get_screen_dpi (GdkScreen *screen)
313 {
314         gdouble dp, di;
315
316         /*diagonal in pixels*/
317         dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
318
319         /*diagonal in inches*/
320         di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;
321
322         return (dp / di);
323 }
324
325 /* Returns a locale specific date and time representation */
326 gchar *
327 ev_document_misc_format_date (GTime utime)
328 {
329         time_t time = (time_t) utime;
330         char s[256];
331         const char fmt_hack[] = "%c";
332         size_t len;
333 #ifdef HAVE_LOCALTIME_R
334         struct tm t;
335         if (time == 0 || !localtime_r (&time, &t)) return NULL;
336         len = strftime (s, sizeof (s), fmt_hack, &t);
337 #else
338         struct tm *t;
339         if (time == 0 || !(t = localtime (&time)) ) return NULL;
340         len = strftime (s, sizeof (s), fmt_hack, t);
341 #endif
342
343         if (len == 0 || s[0] == '\0') return NULL;
344
345         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
346 }