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
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)
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.
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.
28 #include "ev-document-misc.h"
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
36 create_thumbnail_frame (int width,
38 GdkPixbuf *source_pixbuf,
45 int width_r, height_r;
48 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
51 width_r = gdk_pixbuf_get_width (source_pixbuf);
52 height_r = gdk_pixbuf_get_height (source_pixbuf);
58 /* make sure no one is passing us garbage */
59 g_assert (width_r >= 0 && height_r >= 0);
61 retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
66 /* make it black and fill in the middle */
67 data = gdk_pixbuf_get_pixels (retval);
68 rowstride = gdk_pixbuf_get_rowstride (retval);
70 gdk_pixbuf_fill (retval, 0x000000ff);
72 for (i = 1; i < height_r + 1; i++)
73 memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
76 /* copy the source pixbuf */
78 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
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;
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;
98 ev_document_misc_get_thumbnail_frame (int width,
100 GdkPixbuf *source_pixbuf)
102 return create_thumbnail_frame (width, height, source_pixbuf, TRUE);
106 ev_document_misc_get_loading_thumbnail (int width,
108 gboolean inverted_colors)
110 return create_thumbnail_frame (width, height, NULL, !inverted_colors);
114 ev_document_misc_get_page_border_size (gint page_width,
122 if (page_width < 100) {
125 } else if (page_width < 500) {
136 ev_document_misc_paint_one_page (cairo_t *cr,
141 gboolean inverted_colors)
143 GtkStyle *style = gtk_widget_get_style (widget);
144 GtkStateType state = gtk_widget_get_state (widget);
146 gdk_cairo_set_source_color (cr, highlight ? &style->text[state] : &style->dark[state]);
147 gdk_cairo_rectangle (cr, area);
151 cairo_set_source_rgb (cr, 0, 0, 0);
153 cairo_set_source_rgb (cr, 1, 1, 1);
155 area->x + border->left,
156 area->y + border->top,
157 area->width - (border->left + border->right),
158 area->height - (border->top + border->bottom));
161 gdk_cairo_set_source_color (cr, &style->mid[state]);
164 area->y + area->height - (border->bottom - border->top),
165 border->bottom - border->top,
166 border->bottom - border->top);
170 area->x + area->width - (border->right - border->left),
172 border->right - border->left,
173 border->right - border->left);
178 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
180 cairo_surface_t *surface;
183 surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
184 CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
185 gdk_pixbuf_get_width (pixbuf),
186 gdk_pixbuf_get_height (pixbuf));
187 cr = cairo_create (surface);
188 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
196 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
199 cairo_surface_t *image;
203 cairo_format_t surface_format;
204 gint pixbuf_n_channels;
205 gint pixbuf_rowstride;
206 guchar *pixbuf_pixels;
209 width = cairo_image_surface_get_width (surface);
210 height = cairo_image_surface_get_height (surface);
212 surface_format = cairo_image_surface_get_format (surface);
213 has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
215 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
218 pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
219 pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
220 pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
222 image = cairo_image_surface_create_for_data (pixbuf_pixels,
226 cr = cairo_create (image);
227 cairo_set_source_surface (cr, surface, 0, 0);
230 cairo_mask_surface (cr, surface, 0, 0);
235 cairo_surface_destroy (image);
237 for (y = 0; y < height; y++) {
238 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
240 for (x = 0; x < width; x++) {
243 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
247 p[3] = (has_alpha) ? p[3] : 0xff;
253 p[3] = (has_alpha) ? tmp : 0xff;
255 p += pixbuf_n_channels;
263 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
268 cairo_surface_t *new_surface;
271 gint new_width = dest_width;
272 gint new_height = dest_height;
274 width = cairo_image_surface_get_width (surface);
275 height = cairo_image_surface_get_height (surface);
277 if (dest_width == width &&
278 dest_height == height &&
279 dest_rotation == 0) {
280 return cairo_surface_reference (surface);
283 if (dest_rotation == 90 || dest_rotation == 270) {
284 new_width = dest_height;
285 new_height = dest_width;
288 new_surface = cairo_surface_create_similar (surface,
289 cairo_surface_get_content (surface),
290 new_width, new_height);
292 cr = cairo_create (new_surface);
293 switch (dest_rotation) {
295 cairo_translate (cr, new_width, 0);
298 cairo_translate (cr, new_width, new_height);
301 cairo_translate (cr, 0, new_height);
304 cairo_translate (cr, 0, 0);
307 if (dest_width != width || dest_height != height) {
308 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
310 (gdouble)dest_width / width,
311 (gdouble)dest_height / height);
314 cairo_rotate (cr, dest_rotation * G_PI / 180.0);
315 cairo_set_source_surface (cr, surface, 0, 0);
323 ev_document_misc_invert_surface (cairo_surface_t *surface) {
326 cr = cairo_create (surface);
328 /* white + DIFFERENCE -> invert */
329 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
330 cairo_set_source_rgb (cr, 1., 1., 1.);
336 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
339 guint width, height, x, y, rowstride, n_channels;
341 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
342 g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
343 g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
345 /* First grab a pointer to the raw pixel data. */
346 data = gdk_pixbuf_get_pixels (pixbuf);
348 /* Find the number of bytes per row (could be padded). */
349 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
351 width = gdk_pixbuf_get_width (pixbuf);
352 height = gdk_pixbuf_get_height (pixbuf);
353 for (x = 0; x < width; x++) {
354 for (y = 0; y < height; y++) {
355 /* Calculate pixel's offset into the data array. */
356 p = data + x * n_channels + y * rowstride;
357 /* Change the RGB values*/
366 ev_document_misc_get_screen_dpi (GdkScreen *screen)
370 /*diagonal in pixels*/
371 dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
373 /*diagonal in inches*/
374 di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;
379 /* Returns a locale specific date and time representation */
381 ev_document_misc_format_date (GTime utime)
383 time_t time = (time_t) utime;
385 const char fmt_hack[] = "%c";
387 #ifdef HAVE_LOCALTIME_R
389 if (time == 0 || !localtime_r (&time, &t)) return NULL;
390 len = strftime (s, sizeof (s), fmt_hack, &t);
393 if (time == 0 || !(t = localtime (&time)) ) return NULL;
394 len = strftime (s, sizeof (s), fmt_hack, t);
397 if (len == 0 || s[0] == '\0') return NULL;
399 return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);