Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-document-misc.c
Go to the documentation of this file.
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 *
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 
105 GdkPixbuf *
107  int height,
108  GdkPixbuf *source_pixbuf)
109 {
110  return create_thumbnail_frame (width, height, source_pixbuf, TRUE);
111 }
112 
121 GdkPixbuf *
123  int height,
124  gboolean inverted_colors)
125 {
126  return create_thumbnail_frame (width, height, NULL, !inverted_colors);
127 }
128 
129 static cairo_surface_t *
131  int width,
132  int height,
133  gboolean inverted_colors,
134  GdkPixbuf *source_pixbuf,
135  cairo_surface_t *source_surface)
136 {
137  GtkStyleContext *context = gtk_widget_get_style_context (widget);
138  GtkStateFlags state = gtk_widget_get_state_flags (widget);
139  double width_r, height_r;
140  double width_f, height_f;
141  cairo_surface_t *surface;
142  cairo_t *cr;
143  double device_scale_x = 1;
144  double device_scale_y = 1;
145  GtkBorder border = {0, };
146 
147  if (source_surface) {
148  width_r = cairo_image_surface_get_width (source_surface);
149  height_r = cairo_image_surface_get_height (source_surface);
150 #ifdef HAVE_HIDPI_SUPPORT
151  cairo_surface_get_device_scale (source_surface, &device_scale_x, &device_scale_y);
152 #endif
153  } else if (source_pixbuf) {
154  g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
155 
156  width_r = gdk_pixbuf_get_width (source_pixbuf);
157  height_r = gdk_pixbuf_get_height (source_pixbuf);
158 #ifdef HAVE_HIDPI_SUPPORT
159  device_scale_x = device_scale_y = gtk_widget_get_scale_factor (widget);
160 #endif
161  } else {
162  width_r = width;
163  height_r = height;
164 #ifdef HAVE_HIDPI_SUPPORT
165  device_scale_x = device_scale_y = gtk_widget_get_scale_factor (widget);
166 #endif
167  }
168 
169  width_r /= device_scale_x;
170  height_r /= device_scale_y;
171 
172  gtk_style_context_save (context);
173 
174  gtk_style_context_add_class (context, "page-thumbnail");
175  if (inverted_colors)
176  gtk_style_context_add_class (context, "inverted");
177 
178  gtk_style_context_get_border (context, state, &border);
179  width_f = width_r + border.left + border.right;
180  height_f = height_r + border.top + border.bottom;
181 
182  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
183  device_scale_x * width_f,
184  device_scale_y * height_f);
185 
186 #ifdef HAVE_HIDPI_SUPPORT
187  cairo_surface_set_device_scale (surface, device_scale_x, device_scale_y);
188 #endif
189 
190  cr = cairo_create (surface);
191  if (source_surface) {
192  cairo_set_source_surface (cr, source_surface, border.left, border.top);
193  cairo_paint (cr);
194  } else if (source_pixbuf) {
195  gdk_cairo_set_source_pixbuf (cr, source_pixbuf, border.left, border.top);
196  cairo_paint (cr);
197  } else {
198  gtk_render_background (context, cr, 0, 0, width_f, height_f);
199  }
200  gtk_render_frame (context, cr, 0, 0, width_f, height_f);
201  cairo_destroy (cr);
202 
203  gtk_style_context_restore (context);
204 
205  return surface;
206 }
207 
219 GdkPixbuf *
221  int width,
222  int height,
223  gboolean inverted_colors)
224 {
225  GdkPixbuf *retval;
226  cairo_surface_t *surface;
227 
228  surface = ev_document_misc_render_thumbnail_frame (widget, width, height, inverted_colors, NULL, NULL);
229  retval = gdk_pixbuf_get_from_surface (surface, 0, 0, width, height);
230  cairo_surface_destroy (surface);
231 
232  return retval;
233 }
234 
246 cairo_surface_t *
248  int width,
249  int height,
250  gboolean inverted_colors)
251 {
252  return ev_document_misc_render_thumbnail_frame (widget, width, height, inverted_colors, NULL, NULL);
253 }
254 
264 GdkPixbuf *
266  GdkPixbuf *source_pixbuf)
267 {
268  GdkPixbuf *retval;
269  cairo_surface_t *surface;
270 
271  surface = ev_document_misc_render_thumbnail_frame (widget, -1, -1, FALSE, source_pixbuf, NULL);
272  retval = gdk_pixbuf_get_from_surface (surface, 0, 0,
273  cairo_image_surface_get_width (surface),
274  cairo_image_surface_get_height (surface));
275  cairo_surface_destroy (surface);
276 
277  return retval;
278 }
279 
291 cairo_surface_t *
293  cairo_surface_t *source_surface,
294  int width,
295  int height)
296 {
297  return ev_document_misc_render_thumbnail_frame (widget, width, height, FALSE, NULL, source_surface);
298 }
299 
300 void
302  gint page_height,
303  GtkBorder *border)
304 {
305  g_assert (border);
306 
307  border->left = 1;
308  border->top = 1;
309  if (page_width < 100) {
310  border->right = 2;
311  border->bottom = 2;
312  } else if (page_width < 500) {
313  border->right = 3;
314  border->bottom = 3;
315  } else {
316  border->right = 4;
317  border->bottom = 4;
318  }
319 }
320 
321 
322 void
324  GtkWidget *widget,
325  GdkRectangle *area,
326  GtkBorder *border,
327  gboolean highlight,
328  gboolean inverted_colors)
329 {
330  GtkStyleContext *context = gtk_widget_get_style_context (widget);
331  GtkStateFlags state = gtk_widget_get_state_flags (widget);
332  GdkRGBA fg, bg, shade_bg;
333 
334  gtk_style_context_get_background_color (context, state, &bg);
335  gtk_style_context_get_color (context, state, &fg);
336  gtk_style_context_get_color (context, GTK_STATE_FLAG_INSENSITIVE, &shade_bg);
337 
338  gdk_cairo_set_source_rgba (cr, highlight ? &fg : &shade_bg);
339  gdk_cairo_rectangle (cr, area);
340  cairo_fill (cr);
341 
342  if (inverted_colors)
343  cairo_set_source_rgb (cr, 0, 0, 0);
344  else
345  cairo_set_source_rgb (cr, 1, 1, 1);
346  cairo_rectangle (cr,
347  area->x + border->left,
348  area->y + border->top,
349  area->width - (border->left + border->right),
350  area->height - (border->top + border->bottom));
351  cairo_fill (cr);
352 
353  gdk_cairo_set_source_rgba (cr, &bg);
354  cairo_rectangle (cr,
355  area->x,
356  area->y + area->height - (border->bottom - border->top),
357  border->bottom - border->top,
358  border->bottom - border->top);
359  cairo_fill (cr);
360 
361  cairo_rectangle (cr,
362  area->x + area->width - (border->right - border->left),
363  area->y,
364  border->right - border->left,
365  border->right - border->left);
366  cairo_fill (cr);
367 }
368 
369 cairo_surface_t *
371 {
372  cairo_surface_t *surface;
373  cairo_t *cr;
374 
375  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
376 
377  surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
378  CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
379  gdk_pixbuf_get_width (pixbuf),
380  gdk_pixbuf_get_height (pixbuf));
381  cr = cairo_create (surface);
382  gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
383  cairo_paint (cr);
384  cairo_destroy (cr);
385 
386  return surface;
387 }
388 
395 GdkPixbuf *
396 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
397 {
398  g_return_val_if_fail (surface, NULL);
399 
400  return gdk_pixbuf_get_from_surface (surface,
401  0, 0,
402  cairo_image_surface_get_width (surface),
403  cairo_image_surface_get_height (surface));
404 }
405 
406 cairo_surface_t *
408  gint dest_width,
409  gint dest_height,
410  gint dest_rotation)
411 {
412  cairo_surface_t *new_surface;
413  cairo_t *cr;
414  gint width, height;
415  gint new_width = dest_width;
416  gint new_height = dest_height;
417 
418  width = cairo_image_surface_get_width (surface);
419  height = cairo_image_surface_get_height (surface);
420 
421  if (dest_width == width &&
422  dest_height == height &&
423  dest_rotation == 0) {
424  return cairo_surface_reference (surface);
425  }
426 
427  if (dest_rotation == 90 || dest_rotation == 270) {
428  new_width = dest_height;
429  new_height = dest_width;
430  }
431 
432  new_surface = cairo_surface_create_similar (surface,
433  cairo_surface_get_content (surface),
434  new_width, new_height);
435 
436  cr = cairo_create (new_surface);
437  switch (dest_rotation) {
438  case 90:
439  cairo_translate (cr, new_width, 0);
440  break;
441  case 180:
442  cairo_translate (cr, new_width, new_height);
443  break;
444  case 270:
445  cairo_translate (cr, 0, new_height);
446  break;
447  default:
448  cairo_translate (cr, 0, 0);
449  }
450  cairo_rotate (cr, dest_rotation * G_PI / 180.0);
451 
452  if (dest_width != width || dest_height != height) {
453  cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
454  cairo_scale (cr,
455  (gdouble)dest_width / width,
456  (gdouble)dest_height / height);
457  }
458 
459  cairo_set_source_surface (cr, surface, 0, 0);
460  cairo_paint (cr);
461  cairo_destroy (cr);
462 
463  return new_surface;
464 }
465 
466 void
467 ev_document_misc_invert_surface (cairo_surface_t *surface) {
468  cairo_t *cr;
469 
470  cr = cairo_create (surface);
471 
472  /* white + DIFFERENCE -> invert */
473  cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
474  cairo_set_source_rgb (cr, 1., 1., 1.);
475  cairo_paint(cr);
476  cairo_destroy (cr);
477 }
478 
479 void
481 {
482  guchar *data, *p;
483  guint width, height, x, y, rowstride, n_channels;
484 
485  n_channels = gdk_pixbuf_get_n_channels (pixbuf);
486  g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
487  g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
488 
489  /* First grab a pointer to the raw pixel data. */
490  data = gdk_pixbuf_get_pixels (pixbuf);
491 
492  /* Find the number of bytes per row (could be padded). */
493  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
494 
495  width = gdk_pixbuf_get_width (pixbuf);
496  height = gdk_pixbuf_get_height (pixbuf);
497  for (x = 0; x < width; x++) {
498  for (y = 0; y < height; y++) {
499  /* Calculate pixel's offset into the data array. */
500  p = data + x * n_channels + y * rowstride;
501  /* Change the RGB values*/
502  p[0] = 255 - p[0];
503  p[1] = 255 - p[1];
504  p[2] = 255 - p[2];
505  }
506  }
507 }
508 
509 gdouble
511 {
512  gdouble dp, di;
513 
514  /*diagonal in pixels*/
515  dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
516 
517  /*diagonal in inches*/
518  di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;
519 
520  return (dp / di);
521 }
522 
523 /* Returns a locale specific date and time representation */
524 gchar *
526 {
527  time_t time = (time_t) utime;
528  char s[256];
529  const char fmt_hack[] = "%c";
530  size_t len;
531 #pragma GCC diagnostic push
532 #pragma GCC diagnostic ignored "-Wformat-y2k"
533 #ifdef HAVE_LOCALTIME_R
534  struct tm t;
535  if (time == 0 || !localtime_r (&time, &t)) return NULL;
536  len = strftime (s, sizeof (s), fmt_hack, &t);
537 #else
538  struct tm *t;
539  if (time == 0 || !(t = localtime (&time)) ) return NULL;
540  len = strftime (s, sizeof (s), fmt_hack, t);
541 #endif
542 #pragma GCC diagnostic pop
543 
544  if (len == 0 || s[0] == '\0') return NULL;
545 
546  return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
547 }
548 
549 void
551  gint *x,
552  gint *y)
553 {
554  GdkDeviceManager *device_manager;
555  GdkDevice *device_pointer;
556  GdkRectangle allocation;
557 
558  if (x)
559  *x = -1;
560  if (y)
561  *y = -1;
562 
563  if (!gtk_widget_get_realized (widget))
564  return;
565 
566  device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
567  device_pointer = gdk_device_manager_get_client_pointer (device_manager);
568  gdk_window_get_device_position (gtk_widget_get_window (widget),
569  device_pointer,
570  x, y, NULL);
571 
572  if (gtk_widget_get_has_window (widget))
573  return;
574 
575  gtk_widget_get_allocation (widget, &allocation);
576  if (x)
577  *x -= allocation.x;
578  if (y)
579  *y -= allocation.y;
580 }