blob: 1ca8aacc4fc5d4ae16293268e503539029fea1b8 [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>
24
Stepan Salenikovichd8765072016-01-14 10:58:51 -050025static constexpr const char* MSG_COUNT_FONT = "Sans";
26static constexpr int MSG_COUNT_FONT_SIZE = 12;
27static constexpr GdkRGBA MSG_COUNT_FONT_COLOUR = {1.0, 1.0, 1.0, 1.0}; // white
28static constexpr GdkRGBA MSG_COUNT_BACKGROUND = {0.984, 0.282, 0.278, 0.9}; // red 251, 72, 71, 0.9
29
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050030GdkPixbuf *
31ring_draw_fallback_avatar(int size) {
32 cairo_surface_t *surface;
33 cairo_t *cr;
34
35 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
36 cr = cairo_create(surface);
37
38 cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size);
39 cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937);
40 cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969);
41
42 cairo_set_source(cr, linpat);
43 cairo_paint(cr);
44
Stepan Salenikovich2beca292015-08-17 17:03:21 -040045 cairo_pattern_destroy(linpat);
46
Stepan Salenikovich297b5d12015-02-26 17:51:13 -050047 int avatar_size = size * 0.3;
48 GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "avatar-default-symbolic",
49 avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
50 GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
51 g_object_unref(icon_info);
52
53 if (pixbuf_icon != NULL) {
54 gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
55 g_object_unref(pixbuf_icon);
56 cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
57 cairo_fill(cr);
58 }
59
60 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);
61
62 /* free resources */
63 cairo_destroy(cr);
64 cairo_surface_destroy(surface);
65
66 return pixbuf;
67}
68
69GdkPixbuf *
Stepan Salenikovichee8506e2015-08-13 11:35:14 -040070ring_draw_conference_avatar(int size) {
71 cairo_surface_t *surface;
72 cairo_t *cr;
73
74 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
75 cr = cairo_create(surface);
76
77 cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size);
78 cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937);
79 cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969);
80
81 cairo_set_source(cr, linpat);
82 cairo_paint(cr);
83
84 int avatar_size = size * 0.5;
85 GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "system-users-symbolic",
86 avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
87 GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
88 g_object_unref(icon_info);
89
90 if (pixbuf_icon != NULL) {
91 gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
92 g_object_unref(pixbuf_icon);
93 cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
94 cairo_fill(cr);
95 }
96
97 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);
98
99 /* free resources */
100 cairo_destroy(cr);
101 cairo_surface_destroy(surface);
102
103 return pixbuf;
104}
105
106GdkPixbuf *
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500107ring_frame_avatar(GdkPixbuf *avatar) {
108 int extra_space = 10;
109 int offset = extra_space/2;
110 int w = gdk_pixbuf_get_width(avatar);
111 int h = gdk_pixbuf_get_height(avatar);
112 int w_surface = w + extra_space;
113 int h_surface = h + extra_space;
114 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w_surface, h_surface);
115 cairo_t *cr = cairo_create(surface);
116
117 cairo_set_source_rgba(cr, 0, 0, 0, 0);
118 cairo_rectangle(cr, 0, 0, w_surface, h_surface);
119 cairo_fill(cr);
120
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500121 double aspect = (double)w/(double)h;
122 double corner_radius = 5;
123 double radius = corner_radius/aspect;
124 double degrees = M_PI / 180.0;
125
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400126 // create the square path with ronded corners
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500127 cairo_new_sub_path (cr);
128 cairo_arc (cr, offset + w - radius, offset + radius, radius, -90 * degrees, 0 * degrees);
129 cairo_arc (cr, offset + w - radius, offset + h - radius, radius, 0 * degrees, 90 * degrees);
130 cairo_arc (cr, offset + radius, offset + h - radius, radius, 90 * degrees, 180 * degrees);
131 cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees);
132 cairo_close_path (cr);
133
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400134 // in case the image has alpha, we want to first set the background of the part inside the
135 // blue frame to black; otherwise the resulting image will show whatever is in the background,
136 // which can be weird in certain cases (eg: the image displayed over a video)
137 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500138 cairo_fill_preserve(cr);
139
Stepan Salenikovich89e3d9f2016-06-06 11:57:31 -0400140 // now draw the image over this black square
141 gdk_cairo_set_source_pixbuf(cr, avatar, offset, offset);
142 cairo_fill_preserve(cr);
143
144 // now draw the blue frame
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500145 cairo_set_source_rgba (cr, 58.0/256.0, 191/256.0, 210/256.0, 1.0);
146 cairo_set_line_width (cr, 2.0);
147 cairo_stroke (cr);
148
149 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w_surface, h_surface);
150
151 /* free resources */
152 cairo_destroy(cr);
153 cairo_surface_destroy(surface);
154
155 return pixbuf;
Stepan Salenikovich2beca292015-08-17 17:03:21 -0400156}
Stepan Salenikovichd8765072016-01-14 10:58:51 -0500157
158static void
159create_rounded_rectangle_path(cairo_t *cr, double corner_radius, double x, double y, double w, double h)
160{
161 double radius = corner_radius;
162 double degrees = M_PI / 180.0;
163
164 cairo_new_sub_path (cr);
165 cairo_arc (cr, x + w - radius, y + radius, radius, -90 * degrees, 0 * degrees);
166 cairo_arc (cr, x + w - radius, y + h - radius, radius, 0 * degrees, 90 * degrees);
167 cairo_arc (cr, x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees);
168 cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
169 cairo_close_path (cr);
170}
171
172/**
173 * Draws the unread message count in the bottom right corner of the given image.
174 * In the case that the count is less than or equal to 0, nothing is drawn.
175 */
176GdkPixbuf *
177ring_draw_unread_messages(const GdkPixbuf *avatar, int unread_count) {
178 if (unread_count <= 0) {
179 // simply return a copy of the original pixbuf
180 return gdk_pixbuf_copy(avatar);
181 }
182 int w = gdk_pixbuf_get_width(avatar);
183 int h = gdk_pixbuf_get_height(avatar);
184 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
185 cairo_t *cr = cairo_create(surface);
186 cairo_surface_destroy(surface);
187
188 /* draw original image */
189 gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
190 cairo_paint(cr);
191
192 /* make text */
193 char *text = g_strdup_printf("%d", unread_count);
194 cairo_text_extents_t extents;
195
196 cairo_select_font_face (cr, MSG_COUNT_FONT,
197 CAIRO_FONT_SLANT_NORMAL,
198 CAIRO_FONT_WEIGHT_NORMAL);
199
200 cairo_set_font_size (cr, MSG_COUNT_FONT_SIZE);
201 cairo_text_extents (cr, text, &extents);
202
203 /* draw rounded rectangle around the text, with 3 pixel border
204 * ie: 6 pixels higher, 6 pixels wider */
205 int border_width = 3;
206 double rec_x = w - extents.width - border_width * 2;
207 double rec_y = h - extents.height - border_width * 2;
208 double rec_w = extents.width + border_width * 2;
209 double rec_h = extents.height + border_width * 2;
210 double corner_radius = rec_h/2.5;
211 create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);
212 cairo_set_source_rgba(cr, MSG_COUNT_BACKGROUND.red, MSG_COUNT_BACKGROUND.blue, MSG_COUNT_BACKGROUND.green, MSG_COUNT_BACKGROUND.alpha);
213 cairo_fill(cr);
214
215 /* draw text */
216 cairo_move_to (cr, w-extents.width-border_width, h-border_width);
217 cairo_set_source_rgb(cr, MSG_COUNT_FONT_COLOUR.red, MSG_COUNT_FONT_COLOUR.blue, MSG_COUNT_FONT_COLOUR.green);
218 cairo_show_text (cr, text);
219
220 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);
221
222 /* free resources */
223 cairo_destroy(cr);
224 g_free(text);
225
226 return pixbuf;
227}