blob: 50e255b407470dcd5f2e79df55027b355e71deaf [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
Stepan Salenikovich2beca292015-08-17 17:03:21 -040051 cairo_pattern_destroy(linpat);
52
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050053 int avatar_size = size * 0.3;
54 GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "avatar-default-symbolic",
55 avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
56 GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
57 g_object_unref(icon_info);
58
59 if (pixbuf_icon != NULL) {
60 gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
61 g_object_unref(pixbuf_icon);
62 cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
63 cairo_fill(cr);
64 }
65
66 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);
67
68 /* free resources */
69 cairo_destroy(cr);
70 cairo_surface_destroy(surface);
71
72 return pixbuf;
73}
74
75GdkPixbuf *
76ring_frame_avatar(GdkPixbuf *avatar) {
77 int extra_space = 10;
78 int offset = extra_space/2;
79 int w = gdk_pixbuf_get_width(avatar);
80 int h = gdk_pixbuf_get_height(avatar);
81 int w_surface = w + extra_space;
82 int h_surface = h + extra_space;
83 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w_surface, h_surface);
84 cairo_t *cr = cairo_create(surface);
85
86 cairo_set_source_rgba(cr, 0, 0, 0, 0);
87 cairo_rectangle(cr, 0, 0, w_surface, h_surface);
88 cairo_fill(cr);
89
90 gdk_cairo_set_source_pixbuf(cr, avatar, offset, offset);
91
92 double aspect = (double)w/(double)h;
93 double corner_radius = 5;
94 double radius = corner_radius/aspect;
95 double degrees = M_PI / 180.0;
96
97 cairo_new_sub_path (cr);
98 cairo_arc (cr, offset + w - radius, offset + radius, radius, -90 * degrees, 0 * degrees);
99 cairo_arc (cr, offset + w - radius, offset + h - radius, radius, 0 * degrees, 90 * degrees);
100 cairo_arc (cr, offset + radius, offset + h - radius, radius, 90 * degrees, 180 * degrees);
101 cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees);
102 cairo_close_path (cr);
103
104 cairo_fill_preserve(cr);
105
106 cairo_set_source_rgba (cr, 58.0/256.0, 191/256.0, 210/256.0, 1.0);
107 cairo_set_line_width (cr, 2.0);
108 cairo_stroke (cr);
109
110 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w_surface, h_surface);
111
112 /* free resources */
113 cairo_destroy(cr);
114 cairo_surface_destroy(surface);
115
116 return pixbuf;
Stepan Salenikovich2beca292015-08-17 17:03:21 -0400117}