blob: 4fb18f5b80855be47edc79e782736a38b8626bef [file] [log] [blame]
Stepan Salenikovich297b5d12015-02-26 17:51:13 -05001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "drawing.h"
32
33#include <gtk/gtk.h>
34#include <math.h>
35
36GdkPixbuf *
37ring_draw_fallback_avatar(int size) {
38 cairo_surface_t *surface;
39 cairo_t *cr;
40
41 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
42 cr = cairo_create(surface);
43
44 cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size);
45 cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937);
46 cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969);
47
48 cairo_set_source(cr, linpat);
49 cairo_paint(cr);
50
51 int avatar_size = size * 0.3;
52 GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "avatar-default-symbolic",
53 avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
54 GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
55 g_object_unref(icon_info);
56
57 if (pixbuf_icon != NULL) {
58 gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
59 g_object_unref(pixbuf_icon);
60 cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
61 cairo_fill(cr);
62 }
63
64 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);
65
66 /* free resources */
67 cairo_destroy(cr);
68 cairo_surface_destroy(surface);
69
70 return pixbuf;
71}
72
73GdkPixbuf *
74ring_frame_avatar(GdkPixbuf *avatar) {
75 int extra_space = 10;
76 int offset = extra_space/2;
77 int w = gdk_pixbuf_get_width(avatar);
78 int h = gdk_pixbuf_get_height(avatar);
79 int w_surface = w + extra_space;
80 int h_surface = h + extra_space;
81 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w_surface, h_surface);
82 cairo_t *cr = cairo_create(surface);
83
84 cairo_set_source_rgba(cr, 0, 0, 0, 0);
85 cairo_rectangle(cr, 0, 0, w_surface, h_surface);
86 cairo_fill(cr);
87
88 gdk_cairo_set_source_pixbuf(cr, avatar, offset, offset);
89
90 double aspect = (double)w/(double)h;
91 double corner_radius = 5;
92 double radius = corner_radius/aspect;
93 double degrees = M_PI / 180.0;
94
95 cairo_new_sub_path (cr);
96 cairo_arc (cr, offset + w - radius, offset + radius, radius, -90 * degrees, 0 * degrees);
97 cairo_arc (cr, offset + w - radius, offset + h - radius, radius, 0 * degrees, 90 * degrees);
98 cairo_arc (cr, offset + radius, offset + h - radius, radius, 90 * degrees, 180 * degrees);
99 cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees);
100 cairo_close_path (cr);
101
102 cairo_fill_preserve(cr);
103
104 cairo_set_source_rgba (cr, 58.0/256.0, 191/256.0, 210/256.0, 1.0);
105 cairo_set_line_width (cr, 2.0);
106 cairo_stroke (cr);
107
108 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w_surface, h_surface);
109
110 /* free resources */
111 cairo_destroy(cr);
112 cairo_surface_destroy(surface);
113
114 return pixbuf;
115}