blob: 75d584980d5d186168b7f801e91a8fd0f8129e98 [file] [log] [blame]
Stepan Salenikovich297b5d12015-02-26 17:51:13 -05001/*
Guillaume Roguez2a6150d2017-07-19 18:24:47 -04002 * Copyright (C) 2015-2017 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
Sébastien Blin3367b662017-07-24 11:15:19 -0400109#include <iostream>
AmarOkb37e92b2017-07-19 09:40:00 -0400110
Stepan Salenikovichee8506e2015-08-13 11:35:14 -0400111GdkPixbuf *
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500112ring_frame_avatar(GdkPixbuf *avatar) {
AmarOkb37e92b2017-07-19 09:40:00 -0400113
114 auto w = gdk_pixbuf_get_width(avatar);
115 auto h = gdk_pixbuf_get_height(avatar);
Sébastien Blin3367b662017-07-24 11:15:19 -0400116 auto crop_size = std::min(h, w);
117 auto new_size = std::max(h, w);
118 auto scale = (double)new_size/(double)crop_size;
119 GdkPixbuf *crop_avatar = gdk_pixbuf_new (
120 gdk_pixbuf_get_colorspace (avatar),
121 gdk_pixbuf_get_has_alpha (avatar),
122 gdk_pixbuf_get_bits_per_sample (avatar),
123 new_size, new_size);
AmarOkb37e92b2017-07-19 09:40:00 -0400124 gdk_pixbuf_scale (avatar, crop_avatar, 0, 0, new_size, new_size,
Sébastien Blin3367b662017-07-24 11:15:19 -0400125 (w/2)-(new_size/2), (h/2)-(new_size/2), scale, scale,
AmarOkb37e92b2017-07-19 09:40:00 -0400126 GDK_INTERP_BILINEAR);
127 auto extra_space = 10;
128 auto offset = extra_space/2;
129 auto s_surface = new_size + extra_space;
130 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, s_surface, s_surface);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500131 cairo_t *cr = cairo_create(surface);
132
133 cairo_set_source_rgba(cr, 0, 0, 0, 0);
AmarOkb37e92b2017-07-19 09:40:00 -0400134 cairo_rectangle(cr, 0, 0, s_surface, s_surface);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500135 cairo_fill(cr);
136
AmarOkb37e92b2017-07-19 09:40:00 -0400137 double radius = new_size/2;
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500138 double degrees = M_PI / 180.0;
139
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400140 // create the square path with ronded corners
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500141 cairo_new_sub_path (cr);
AmarOkb37e92b2017-07-19 09:40:00 -0400142 cairo_arc (cr, offset + new_size - radius, offset + radius, radius, -90 * degrees, 0 * degrees);
143 cairo_arc (cr, offset + new_size - radius, offset + new_size - radius, radius, 0 * degrees, 90 * degrees);
144 cairo_arc (cr, offset + radius, offset + new_size - radius, radius, 90 * degrees, 180 * degrees);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500145 cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees);
146 cairo_close_path (cr);
147
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400148 // 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 -0400149 // blue frame to white; otherwise the resulting image will show whatever is in the background,
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400150 // which can be weird in certain cases (eg: the image displayed over a video)
Stepan Salenikovich1dc123f2016-09-14 10:43:10 -0400151 cairo_set_source_rgba(cr, 1, 1, 1, 1);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500152 cairo_fill_preserve(cr);
153
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400154 // now draw the image over this black square
AmarOkb37e92b2017-07-19 09:40:00 -0400155 gdk_cairo_set_source_pixbuf(cr, crop_avatar, offset, offset);
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400156 cairo_fill_preserve(cr);
157
AmarOkb37e92b2017-07-19 09:40:00 -0400158 auto pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, s_surface, s_surface);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500159
160 /* free resources */
161 cairo_destroy(cr);
162 cairo_surface_destroy(surface);
163
164 return pixbuf;
Stepan Salenikovich2beca292015-08-17 17:03:21 -0400165}
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500166
167static void
168create_rounded_rectangle_path(cairo_t *cr, double corner_radius, double x, double y, double w, double h)
169{
170 double radius = corner_radius;
171 double degrees = M_PI / 180.0;
172
173 cairo_new_sub_path (cr);
174 cairo_arc (cr, x + w - radius, y + radius, radius, -90 * degrees, 0 * degrees);
175 cairo_arc (cr, x + w - radius, y + h - radius, radius, 0 * degrees, 90 * degrees);
176 cairo_arc (cr, x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees);
177 cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
178 cairo_close_path (cr);
179}
180
181/**
aviauc372e812016-12-01 16:13:16 -0500182 * Draws the presence icon in the top right corner of the given image.
183 */
184GdkPixbuf *
185ring_draw_presence(const GdkPixbuf *avatar, bool present) {
186 if (!present) {
187 // simply return a copy of the original pixbuf
188 return gdk_pixbuf_copy(avatar);
189 }
190
191 int w = gdk_pixbuf_get_width(avatar);
192 int h = gdk_pixbuf_get_height(avatar);
193 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
194 cairo_t *cr = cairo_create(surface);
195 cairo_surface_destroy(surface);
196
197 /* draw original image */
198 gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
199 cairo_paint(cr);
200
201 /* draw rounded rectangle, with 3 pixel border
202 * ie: 6 pixels higher, 6 pixels wider */
203 int border_width = 5;
AmarOkb37e92b2017-07-19 09:40:00 -0400204 double rec_x = w - border_width * 3;
205 double rec_y = h - border_width * 3;
aviauc372e812016-12-01 16:13:16 -0500206 double rec_w = border_width * 2;
207 double rec_h = border_width * 2;
208 double corner_radius = rec_h/2.5;
209 create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);
210
211 // For now we don't draw the absent background.
212 auto background = present ? PRESENCE_PRESENT_BACKGROUND : PRESENCE_ABSENT_BACKGROUND;
213 cairo_set_source_rgba(
214 cr,
215 background.red,
216 background.blue,
217 background.green,
218 background.alpha
219 );
AmarOkb37e92b2017-07-19 09:40:00 -0400220 cairo_fill_preserve(cr);
221 cairo_set_source_rgb(cr, 1, 1, 1);
222 cairo_set_line_width(cr, 1.2);
223 cairo_stroke(cr);
aviauc372e812016-12-01 16:13:16 -0500224
225 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);
226
227 /* free resources */
228 cairo_destroy(cr);
229
230 return pixbuf;
231}
232
233/**
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500234 * Draws the unread message count in the bottom right corner of the given image.
235 * In the case that the count is less than or equal to 0, nothing is drawn.
236 */
237GdkPixbuf *
238ring_draw_unread_messages(const GdkPixbuf *avatar, int unread_count) {
239 if (unread_count <= 0) {
240 // simply return a copy of the original pixbuf
241 return gdk_pixbuf_copy(avatar);
242 }
243 int w = gdk_pixbuf_get_width(avatar);
244 int h = gdk_pixbuf_get_height(avatar);
245 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
246 cairo_t *cr = cairo_create(surface);
247 cairo_surface_destroy(surface);
248
249 /* draw original image */
250 gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
251 cairo_paint(cr);
252
253 /* make text */
254 char *text = g_strdup_printf("%d", unread_count);
255 cairo_text_extents_t extents;
256
257 cairo_select_font_face (cr, MSG_COUNT_FONT,
258 CAIRO_FONT_SLANT_NORMAL,
259 CAIRO_FONT_WEIGHT_NORMAL);
260
261 cairo_set_font_size (cr, MSG_COUNT_FONT_SIZE);
262 cairo_text_extents (cr, text, &extents);
263
264 /* draw rounded rectangle around the text, with 3 pixel border
265 * ie: 6 pixels higher, 6 pixels wider */
266 int border_width = 3;
267 double rec_x = w - extents.width - border_width * 2;
268 double rec_y = h - extents.height - border_width * 2;
269 double rec_w = extents.width + border_width * 2;
270 double rec_h = extents.height + border_width * 2;
271 double corner_radius = rec_h/2.5;
272 create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);
273 cairo_set_source_rgba(cr, MSG_COUNT_BACKGROUND.red, MSG_COUNT_BACKGROUND.blue, MSG_COUNT_BACKGROUND.green, MSG_COUNT_BACKGROUND.alpha);
274 cairo_fill(cr);
275
276 /* draw text */
277 cairo_move_to (cr, w-extents.width-border_width, h-border_width);
278 cairo_set_source_rgb(cr, MSG_COUNT_FONT_COLOUR.red, MSG_COUNT_FONT_COLOUR.blue, MSG_COUNT_FONT_COLOUR.green);
279 cairo_show_text (cr, text);
280
281 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);
282
283 /* free resources */
284 cairo_destroy(cr);
285 g_free(text);
286
287 return pixbuf;
288}