blob: cbc97d363d12fd564d7b8979222203660ffb7677 [file] [log] [blame]
Stepan Salenikovich297b5d12015-02-26 17:51:13 -05001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich297b5d12015-02-26 17:51:13 -05003 * 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.
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050018 */
19
20#include "drawing.h"
21
22#include <gtk/gtk.h>
23#include <math.h>
AmarOkb37e92b2017-07-19 09:40:00 -040024#include <algorithm>
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050025
Stepan Salenikovichd8765072016-01-14 10:58:51 -050026static constexpr const char* MSG_COUNT_FONT = "Sans";
27static constexpr int MSG_COUNT_FONT_SIZE = 12;
28static constexpr GdkRGBA MSG_COUNT_FONT_COLOUR = {1.0, 1.0, 1.0, 1.0}; // white
29static constexpr GdkRGBA MSG_COUNT_BACKGROUND = {0.984, 0.282, 0.278, 0.9}; // red 251, 72, 71, 0.9
AmarOkb37e92b2017-07-19 09:40:00 -040030static constexpr GdkRGBA PRESENCE_PRESENT_BACKGROUND = {0, 0.4156827, 0.8, 1.0}; // green 112, 217, 6, 0.9
31static constexpr GdkRGBA PRESENCE_ABSENT_BACKGROUND = {0.984, 0.282, 0.278, 1.0}; // red 251, 72, 71, 0.9
Stepan Salenikovichd8765072016-01-14 10:58:51 -050032
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050033GdkPixbuf *
34ring_draw_fallback_avatar(int size) {
35 cairo_surface_t *surface;
36 cairo_t *cr;
37
38 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
39 cr = cairo_create(surface);
40
41 cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size);
42 cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937);
43 cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969);
44
45 cairo_set_source(cr, linpat);
46 cairo_paint(cr);
47
Stepan Salenikovich2beca292015-08-17 17:03:21 -040048 cairo_pattern_destroy(linpat);
49
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050050 int avatar_size = size * 0.3;
51 GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "avatar-default-symbolic",
52 avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
53 GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
54 g_object_unref(icon_info);
55
56 if (pixbuf_icon != NULL) {
57 gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
58 g_object_unref(pixbuf_icon);
59 cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
60 cairo_fill(cr);
61 }
62
63 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);
64
65 /* free resources */
66 cairo_destroy(cr);
67 cairo_surface_destroy(surface);
68
69 return pixbuf;
70}
71
72GdkPixbuf *
Stepan Salenikovichee8506e2015-08-13 11:35:14 -040073ring_draw_conference_avatar(int size) {
74 cairo_surface_t *surface;
75 cairo_t *cr;
76
77 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
78 cr = cairo_create(surface);
79
80 cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size);
81 cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937);
82 cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969);
83
84 cairo_set_source(cr, linpat);
85 cairo_paint(cr);
86
87 int avatar_size = size * 0.5;
88 GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "system-users-symbolic",
89 avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
90 GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
91 g_object_unref(icon_info);
92
93 if (pixbuf_icon != NULL) {
94 gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
95 g_object_unref(pixbuf_icon);
96 cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
97 cairo_fill(cr);
98 }
99
100 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);
101
102 /* free resources */
103 cairo_destroy(cr);
104 cairo_surface_destroy(surface);
105
106 return pixbuf;
107}
108
AmarOkb37e92b2017-07-19 09:40:00 -0400109
Stepan Salenikovichee8506e2015-08-13 11:35:14 -0400110GdkPixbuf *
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500111ring_frame_avatar(GdkPixbuf *avatar) {
AmarOkb37e92b2017-07-19 09:40:00 -0400112
113 auto w = gdk_pixbuf_get_width(avatar);
114 auto h = gdk_pixbuf_get_height(avatar);
115 auto new_size = std::min(h, w);
116 GdkPixbuf *crop_avatar = avatar;
117 gdk_pixbuf_scale (avatar, crop_avatar, 0, 0, new_size, new_size,
118 (w/2)-(new_size/2), (h/2)-(new_size/2), 0, 0,
119 GDK_INTERP_BILINEAR);
120 auto extra_space = 10;
121 auto offset = extra_space/2;
122 auto s_surface = new_size + extra_space;
123 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, s_surface, s_surface);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500124 cairo_t *cr = cairo_create(surface);
125
126 cairo_set_source_rgba(cr, 0, 0, 0, 0);
AmarOkb37e92b2017-07-19 09:40:00 -0400127 cairo_rectangle(cr, 0, 0, s_surface, s_surface);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500128 cairo_fill(cr);
129
AmarOkb37e92b2017-07-19 09:40:00 -0400130 double radius = new_size/2;
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500131 double degrees = M_PI / 180.0;
132
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400133 // create the square path with ronded corners
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500134 cairo_new_sub_path (cr);
AmarOkb37e92b2017-07-19 09:40:00 -0400135 cairo_arc (cr, offset + new_size - radius, offset + radius, radius, -90 * degrees, 0 * degrees);
136 cairo_arc (cr, offset + new_size - radius, offset + new_size - radius, radius, 0 * degrees, 90 * degrees);
137 cairo_arc (cr, offset + radius, offset + new_size - radius, radius, 90 * degrees, 180 * degrees);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500138 cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees);
139 cairo_close_path (cr);
140
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400141 // in case the image has alpha, we want to first set the background of the part inside the
Stepan Salenikovich1dc123f2016-09-14 10:43:10 -0400142 // blue frame to white; otherwise the resulting image will show whatever is in the background,
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400143 // which can be weird in certain cases (eg: the image displayed over a video)
Stepan Salenikovich1dc123f2016-09-14 10:43:10 -0400144 cairo_set_source_rgba(cr, 1, 1, 1, 1);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500145 cairo_fill_preserve(cr);
146
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400147 // now draw the image over this black square
AmarOkb37e92b2017-07-19 09:40:00 -0400148 gdk_cairo_set_source_pixbuf(cr, crop_avatar, offset, offset);
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400149 cairo_fill_preserve(cr);
150
AmarOkb37e92b2017-07-19 09:40:00 -0400151 auto pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, s_surface, s_surface);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500152
153 /* free resources */
154 cairo_destroy(cr);
155 cairo_surface_destroy(surface);
156
157 return pixbuf;
Stepan Salenikovich2beca292015-08-17 17:03:21 -0400158}
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500159
160static void
161create_rounded_rectangle_path(cairo_t *cr, double corner_radius, double x, double y, double w, double h)
162{
163 double radius = corner_radius;
164 double degrees = M_PI / 180.0;
165
166 cairo_new_sub_path (cr);
167 cairo_arc (cr, x + w - radius, y + radius, radius, -90 * degrees, 0 * degrees);
168 cairo_arc (cr, x + w - radius, y + h - radius, radius, 0 * degrees, 90 * degrees);
169 cairo_arc (cr, x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees);
170 cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
171 cairo_close_path (cr);
172}
173
174/**
aviauc372e812016-12-01 16:13:16 -0500175 * Draws the presence icon in the top right corner of the given image.
176 */
177GdkPixbuf *
178ring_draw_presence(const GdkPixbuf *avatar, bool present) {
179 if (!present) {
180 // simply return a copy of the original pixbuf
181 return gdk_pixbuf_copy(avatar);
182 }
183
184 int w = gdk_pixbuf_get_width(avatar);
185 int h = gdk_pixbuf_get_height(avatar);
186 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
187 cairo_t *cr = cairo_create(surface);
188 cairo_surface_destroy(surface);
189
190 /* draw original image */
191 gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
192 cairo_paint(cr);
193
194 /* draw rounded rectangle, with 3 pixel border
195 * ie: 6 pixels higher, 6 pixels wider */
196 int border_width = 5;
AmarOkb37e92b2017-07-19 09:40:00 -0400197 double rec_x = w - border_width * 3;
198 double rec_y = h - border_width * 3;
aviauc372e812016-12-01 16:13:16 -0500199 double rec_w = border_width * 2;
200 double rec_h = border_width * 2;
201 double corner_radius = rec_h/2.5;
202 create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);
203
204 // For now we don't draw the absent background.
205 auto background = present ? PRESENCE_PRESENT_BACKGROUND : PRESENCE_ABSENT_BACKGROUND;
206 cairo_set_source_rgba(
207 cr,
208 background.red,
209 background.blue,
210 background.green,
211 background.alpha
212 );
AmarOkb37e92b2017-07-19 09:40:00 -0400213 cairo_fill_preserve(cr);
214 cairo_set_source_rgb(cr, 1, 1, 1);
215 cairo_set_line_width(cr, 1.2);
216 cairo_stroke(cr);
aviauc372e812016-12-01 16:13:16 -0500217
218 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);
219
220 /* free resources */
221 cairo_destroy(cr);
222
223 return pixbuf;
224}
225
226/**
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500227 * Draws the unread message count in the bottom right corner of the given image.
228 * In the case that the count is less than or equal to 0, nothing is drawn.
229 */
230GdkPixbuf *
231ring_draw_unread_messages(const GdkPixbuf *avatar, int unread_count) {
232 if (unread_count <= 0) {
233 // simply return a copy of the original pixbuf
234 return gdk_pixbuf_copy(avatar);
235 }
236 int w = gdk_pixbuf_get_width(avatar);
237 int h = gdk_pixbuf_get_height(avatar);
238 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
239 cairo_t *cr = cairo_create(surface);
240 cairo_surface_destroy(surface);
241
242 /* draw original image */
243 gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
244 cairo_paint(cr);
245
246 /* make text */
247 char *text = g_strdup_printf("%d", unread_count);
248 cairo_text_extents_t extents;
249
250 cairo_select_font_face (cr, MSG_COUNT_FONT,
251 CAIRO_FONT_SLANT_NORMAL,
252 CAIRO_FONT_WEIGHT_NORMAL);
253
254 cairo_set_font_size (cr, MSG_COUNT_FONT_SIZE);
255 cairo_text_extents (cr, text, &extents);
256
257 /* draw rounded rectangle around the text, with 3 pixel border
258 * ie: 6 pixels higher, 6 pixels wider */
259 int border_width = 3;
260 double rec_x = w - extents.width - border_width * 2;
261 double rec_y = h - extents.height - border_width * 2;
262 double rec_w = extents.width + border_width * 2;
263 double rec_h = extents.height + border_width * 2;
264 double corner_radius = rec_h/2.5;
265 create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);
266 cairo_set_source_rgba(cr, MSG_COUNT_BACKGROUND.red, MSG_COUNT_BACKGROUND.blue, MSG_COUNT_BACKGROUND.green, MSG_COUNT_BACKGROUND.alpha);
267 cairo_fill(cr);
268
269 /* draw text */
270 cairo_move_to (cr, w-extents.width-border_width, h-border_width);
271 cairo_set_source_rgb(cr, MSG_COUNT_FONT_COLOUR.red, MSG_COUNT_FONT_COLOUR.blue, MSG_COUNT_FONT_COLOUR.green);
272 cairo_show_text (cr, text);
273
274 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);
275
276 /* free resources */
277 cairo_destroy(cr);
278 g_free(text);
279
280 return pixbuf;
281}