Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 1 | /* |
Stepan Salenikovich | be87d2c | 2016-01-25 14:14:34 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 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. |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "drawing.h" |
| 21 | |
| 22 | #include <gtk/gtk.h> |
| 23 | #include <math.h> |
| 24 | |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 25 | static constexpr const char* MSG_COUNT_FONT = "Sans"; |
| 26 | static constexpr int MSG_COUNT_FONT_SIZE = 12; |
| 27 | static constexpr GdkRGBA MSG_COUNT_FONT_COLOUR = {1.0, 1.0, 1.0, 1.0}; // white |
| 28 | static constexpr GdkRGBA MSG_COUNT_BACKGROUND = {0.984, 0.282, 0.278, 0.9}; // red 251, 72, 71, 0.9 |
aviau | c372e81 | 2016-12-01 16:13:16 -0500 | [diff] [blame^] | 29 | static constexpr GdkRGBA PRESENCE_PRESENT_BACKGROUND = {0.4375, 0.0234375, 0.84765625, 0.9}; // green 112, 217, 6, 0.9 |
| 30 | static constexpr GdkRGBA PRESENCE_ABSENT_BACKGROUND = {0.984, 0.282, 0.278, 0.9}; // red 251, 72, 71, 0.9 |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 31 | |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 32 | GdkPixbuf * |
| 33 | ring_draw_fallback_avatar(int size) { |
| 34 | cairo_surface_t *surface; |
| 35 | cairo_t *cr; |
| 36 | |
| 37 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size); |
| 38 | cr = cairo_create(surface); |
| 39 | |
| 40 | cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size); |
| 41 | cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937); |
| 42 | cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969); |
| 43 | |
| 44 | cairo_set_source(cr, linpat); |
| 45 | cairo_paint(cr); |
| 46 | |
Stepan Salenikovich | 2beca29 | 2015-08-17 17:03:21 -0400 | [diff] [blame] | 47 | cairo_pattern_destroy(linpat); |
| 48 | |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 49 | int avatar_size = size * 0.3; |
| 50 | GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "avatar-default-symbolic", |
| 51 | avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK); |
| 52 | GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL); |
| 53 | g_object_unref(icon_info); |
| 54 | |
| 55 | if (pixbuf_icon != NULL) { |
| 56 | gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2); |
| 57 | g_object_unref(pixbuf_icon); |
| 58 | cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size); |
| 59 | cairo_fill(cr); |
| 60 | } |
| 61 | |
| 62 | GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size); |
| 63 | |
| 64 | /* free resources */ |
| 65 | cairo_destroy(cr); |
| 66 | cairo_surface_destroy(surface); |
| 67 | |
| 68 | return pixbuf; |
| 69 | } |
| 70 | |
| 71 | GdkPixbuf * |
Stepan Salenikovich | ee8506e | 2015-08-13 11:35:14 -0400 | [diff] [blame] | 72 | ring_draw_conference_avatar(int size) { |
| 73 | cairo_surface_t *surface; |
| 74 | cairo_t *cr; |
| 75 | |
| 76 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size); |
| 77 | cr = cairo_create(surface); |
| 78 | |
| 79 | cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size); |
| 80 | cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937); |
| 81 | cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969); |
| 82 | |
| 83 | cairo_set_source(cr, linpat); |
| 84 | cairo_paint(cr); |
| 85 | |
| 86 | int avatar_size = size * 0.5; |
| 87 | GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "system-users-symbolic", |
| 88 | avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK); |
| 89 | GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL); |
| 90 | g_object_unref(icon_info); |
| 91 | |
| 92 | if (pixbuf_icon != NULL) { |
| 93 | gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2); |
| 94 | g_object_unref(pixbuf_icon); |
| 95 | cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size); |
| 96 | cairo_fill(cr); |
| 97 | } |
| 98 | |
| 99 | GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size); |
| 100 | |
| 101 | /* free resources */ |
| 102 | cairo_destroy(cr); |
| 103 | cairo_surface_destroy(surface); |
| 104 | |
| 105 | return pixbuf; |
| 106 | } |
| 107 | |
| 108 | GdkPixbuf * |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 109 | ring_frame_avatar(GdkPixbuf *avatar) { |
| 110 | int extra_space = 10; |
| 111 | int offset = extra_space/2; |
| 112 | int w = gdk_pixbuf_get_width(avatar); |
| 113 | int h = gdk_pixbuf_get_height(avatar); |
| 114 | int w_surface = w + extra_space; |
| 115 | int h_surface = h + extra_space; |
| 116 | cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w_surface, h_surface); |
| 117 | cairo_t *cr = cairo_create(surface); |
| 118 | |
| 119 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 120 | cairo_rectangle(cr, 0, 0, w_surface, h_surface); |
| 121 | cairo_fill(cr); |
| 122 | |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 123 | double aspect = (double)w/(double)h; |
| 124 | double corner_radius = 5; |
| 125 | double radius = corner_radius/aspect; |
| 126 | double degrees = M_PI / 180.0; |
| 127 | |
Stepan Salenikovich | 89e3d9f | 2016-06-06 11:57:31 -0400 | [diff] [blame] | 128 | // create the square path with ronded corners |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 129 | cairo_new_sub_path (cr); |
| 130 | cairo_arc (cr, offset + w - radius, offset + radius, radius, -90 * degrees, 0 * degrees); |
| 131 | cairo_arc (cr, offset + w - radius, offset + h - radius, radius, 0 * degrees, 90 * degrees); |
| 132 | cairo_arc (cr, offset + radius, offset + h - radius, radius, 90 * degrees, 180 * degrees); |
| 133 | cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees); |
| 134 | cairo_close_path (cr); |
| 135 | |
Stepan Salenikovich | 89e3d9f | 2016-06-06 11:57:31 -0400 | [diff] [blame] | 136 | // in case the image has alpha, we want to first set the background of the part inside the |
Stepan Salenikovich | 1dc123f | 2016-09-14 10:43:10 -0400 | [diff] [blame] | 137 | // blue frame to white; otherwise the resulting image will show whatever is in the background, |
Stepan Salenikovich | 89e3d9f | 2016-06-06 11:57:31 -0400 | [diff] [blame] | 138 | // which can be weird in certain cases (eg: the image displayed over a video) |
Stepan Salenikovich | 1dc123f | 2016-09-14 10:43:10 -0400 | [diff] [blame] | 139 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 140 | cairo_fill_preserve(cr); |
| 141 | |
Stepan Salenikovich | 89e3d9f | 2016-06-06 11:57:31 -0400 | [diff] [blame] | 142 | // now draw the image over this black square |
| 143 | gdk_cairo_set_source_pixbuf(cr, avatar, offset, offset); |
| 144 | cairo_fill_preserve(cr); |
| 145 | |
| 146 | // now draw the blue frame |
Stepan Salenikovich | 297b5d1 | 2015-02-26 17:51:13 -0500 | [diff] [blame] | 147 | cairo_set_source_rgba (cr, 58.0/256.0, 191/256.0, 210/256.0, 1.0); |
| 148 | cairo_set_line_width (cr, 2.0); |
| 149 | cairo_stroke (cr); |
| 150 | |
| 151 | GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w_surface, h_surface); |
| 152 | |
| 153 | /* free resources */ |
| 154 | cairo_destroy(cr); |
| 155 | cairo_surface_destroy(surface); |
| 156 | |
| 157 | return pixbuf; |
Stepan Salenikovich | 2beca29 | 2015-08-17 17:03:21 -0400 | [diff] [blame] | 158 | } |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 159 | |
| 160 | static void |
| 161 | create_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 | /** |
aviau | c372e81 | 2016-12-01 16:13:16 -0500 | [diff] [blame^] | 175 | * Draws the presence icon in the top right corner of the given image. |
| 176 | */ |
| 177 | GdkPixbuf * |
| 178 | ring_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; |
| 197 | double rec_x = w - border_width * 2.5; |
| 198 | double rec_y = h - border_width * 2.5; |
| 199 | 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 | ); |
| 213 | cairo_fill(cr); |
| 214 | |
| 215 | GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h); |
| 216 | |
| 217 | /* free resources */ |
| 218 | cairo_destroy(cr); |
| 219 | |
| 220 | return pixbuf; |
| 221 | } |
| 222 | |
| 223 | /** |
Stepan Salenikovich | d876507 | 2016-01-14 10:58:51 -0500 | [diff] [blame] | 224 | * Draws the unread message count in the bottom right corner of the given image. |
| 225 | * In the case that the count is less than or equal to 0, nothing is drawn. |
| 226 | */ |
| 227 | GdkPixbuf * |
| 228 | ring_draw_unread_messages(const GdkPixbuf *avatar, int unread_count) { |
| 229 | if (unread_count <= 0) { |
| 230 | // simply return a copy of the original pixbuf |
| 231 | return gdk_pixbuf_copy(avatar); |
| 232 | } |
| 233 | int w = gdk_pixbuf_get_width(avatar); |
| 234 | int h = gdk_pixbuf_get_height(avatar); |
| 235 | cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h); |
| 236 | cairo_t *cr = cairo_create(surface); |
| 237 | cairo_surface_destroy(surface); |
| 238 | |
| 239 | /* draw original image */ |
| 240 | gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0); |
| 241 | cairo_paint(cr); |
| 242 | |
| 243 | /* make text */ |
| 244 | char *text = g_strdup_printf("%d", unread_count); |
| 245 | cairo_text_extents_t extents; |
| 246 | |
| 247 | cairo_select_font_face (cr, MSG_COUNT_FONT, |
| 248 | CAIRO_FONT_SLANT_NORMAL, |
| 249 | CAIRO_FONT_WEIGHT_NORMAL); |
| 250 | |
| 251 | cairo_set_font_size (cr, MSG_COUNT_FONT_SIZE); |
| 252 | cairo_text_extents (cr, text, &extents); |
| 253 | |
| 254 | /* draw rounded rectangle around the text, with 3 pixel border |
| 255 | * ie: 6 pixels higher, 6 pixels wider */ |
| 256 | int border_width = 3; |
| 257 | double rec_x = w - extents.width - border_width * 2; |
| 258 | double rec_y = h - extents.height - border_width * 2; |
| 259 | double rec_w = extents.width + border_width * 2; |
| 260 | double rec_h = extents.height + border_width * 2; |
| 261 | double corner_radius = rec_h/2.5; |
| 262 | create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h); |
| 263 | cairo_set_source_rgba(cr, MSG_COUNT_BACKGROUND.red, MSG_COUNT_BACKGROUND.blue, MSG_COUNT_BACKGROUND.green, MSG_COUNT_BACKGROUND.alpha); |
| 264 | cairo_fill(cr); |
| 265 | |
| 266 | /* draw text */ |
| 267 | cairo_move_to (cr, w-extents.width-border_width, h-border_width); |
| 268 | cairo_set_source_rgb(cr, MSG_COUNT_FONT_COLOUR.red, MSG_COUNT_FONT_COLOUR.blue, MSG_COUNT_FONT_COLOUR.green); |
| 269 | cairo_show_text (cr, text); |
| 270 | |
| 271 | GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h); |
| 272 | |
| 273 | /* free resources */ |
| 274 | cairo_destroy(cr); |
| 275 | g_free(text); |
| 276 | |
| 277 | return pixbuf; |
| 278 | } |