drawing.cpp 13.5 KB
Newer Older
Stepan Salenikovich's avatar
Stepan Salenikovich committed
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
Stepan Salenikovich's avatar
Stepan Salenikovich committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#include "drawing.h"

#include <gtk/gtk.h>
#include <math.h>
24
#include <algorithm>
Stepan Salenikovich's avatar
Stepan Salenikovich committed
25

26 27 28
static constexpr const char* MSG_COUNT_FONT        = "Sans";
static constexpr int         MSG_COUNT_FONT_SIZE   = 12;
static constexpr GdkRGBA     MSG_COUNT_FONT_COLOUR = {1.0, 1.0, 1.0, 1.0}; // white
29
static constexpr GdkRGBA     MSG_COUNT_BACKGROUND  = {0.984, 0.282, 0.278, 1.0}; // red 251, 72, 71, 1.0
30 31 32
static constexpr GdkRGBA     GREEN_BACKGROUND = {0.298, 0.392, 0.85, 1.0}; // green 112, 217, 6, 0.9
static constexpr GdkRGBA     ORANGE_BACKGROUND = {1.0, 0.0, 0.5, 1.0}; // orange 255, 128, 0, 1.0
static constexpr GdkRGBA     RED_BACKGROUND = {1.0, 0.156, 0.231, 1.0}; // red 255, 59, 40, 0.9
33 34 35 36
// This is the color palette for default avatars
static constexpr GdkRGBA     COLOR_PALETTE[] = {{0.956862, 0.262745, 0.211764, 1.0}, // red 244, green 67, blue 54, 1 (red)
                                                {0.913725, 0.117647, 0.388235, 1.0}, // red 233, green 30, blue 99, 1 (pink)
                                                {0.611764, 0.152941, 0.690196, 1.0}, // red 156, green 39, blue 176, 1 (purple)
37
                                                {0.270588, 0.152941, 0.627450, 1.0}, // red 69, green 39, blue 160, 1 (deep purple)
38 39 40
                                                {0.403921, 0.227450, 0.717647, 1.0}, // red 103, green 58, blue 183, 1 (indigo)
                                                {0.247058, 0.317647, 0.211764, 1.0}, // red 63, green 81, blue 54, 1 (blue)
                                                {0, 0.737254, 0.831372, 1.0},        // red 0, green 188, blue 212, 1 (cyan)
41
                                                {0, 0.588235, 0.533333, 1.0},        // red 0, green 150, blue 136, 1 (teal)
42 43

                                                {0.298039, 0.682745, 0.313725, 1.0}, // red 76, green 175, blue 80, 1 (green)
44 45 46 47 48 49 50
                                                {0.545098, 0.764705, 0.290196, 1.0}, // red 138, green 194, blue 73, 1 (light green)
                                                {0.619607, 0.619607, 0.619607, 1.0}, // red 157, green 157, blue 157, 1 (grey)
                                                {0.803921, 0.862745, 0.223529, 1.0}, // red 204, green 219, blue 56, 1 (lime)
                                                {1, 0.756862, 0.027450, 1.0},        // red 255, green 192, blue 6, 1 (amber)
                                                {1, 0.341176, 0.133333, 1.0},        // red 255, green 86, blue 33, 1 (deep orange)
                                                {0.474509, 0.333333, 0.282352, 1.0}, // red 120, green 84, blue 71, 1 (brown)
                                                {0.376470, 0.490196, 0.545098, 1.0}};// red 95, green 124, blue 138, 1 (blue grey)
51

Stepan Salenikovich's avatar
Stepan Salenikovich committed
52
GdkPixbuf *
53
ring_draw_fallback_avatar(int size, const std::string& letter, const char color) {
Stepan Salenikovich's avatar
Stepan Salenikovich committed
54 55 56
    cairo_surface_t *surface;
    cairo_t *cr;

57
    // Fill the background
Stepan Salenikovich's avatar
Stepan Salenikovich committed
58 59
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
    cr = cairo_create(surface);
60 61
    auto bg_color = COLOR_PALETTE[color % 16];
    cairo_set_source_rgb (cr, bg_color.red, bg_color.green, bg_color.blue);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
62 63
    cairo_paint(cr);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if (!letter.empty()) {
        // Draw a letter at the center of the avatar
        cairo_text_extents_t extents;
        cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
        cairo_set_font_size(cr, size / 2);
        cairo_set_source_rgb(cr, 1, 1, 1);
        cairo_text_extents(cr, letter.c_str(), &extents);
        auto x = size/2-(extents.width/2 + extents.x_bearing);
        auto y = size/2-(extents.height/2 + extents.y_bearing);
        cairo_move_to(cr, x, y);
        cairo_show_text(cr, letter.c_str());
    } else {
        // Compose from fallback svg if no letter found
        GError *error = nullptr;
        auto* finalAvatar = gdk_pixbuf_get_from_surface(cairo_get_target(cr), 0, 0, size, size);
79
        auto* fallbackavatar = gdk_pixbuf_new_from_resource_at_scale("/net/jami/JamiGnome/fallbackavatar", size, size, true, &error);
80 81 82 83 84
        gdk_pixbuf_composite (fallbackavatar, finalAvatar, 0, 0, size, size, 0, 0, 1, 1, GDK_INTERP_BILINEAR, 0xff);

        return finalAvatar;
    }

85 86

    GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(cairo_get_target(cr), 0, 0, size, size);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    /* free resources */
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    return pixbuf;
}

GdkPixbuf *
ring_draw_conference_avatar(int size) {
    cairo_surface_t *surface;
    cairo_t *cr;

    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
    cr = cairo_create(surface);

    cairo_pattern_t *linpat = cairo_pattern_create_linear(0, 0, 0, size);
    cairo_pattern_add_color_stop_rgb(linpat, 0, 0.937, 0.937, 0.937);
    cairo_pattern_add_color_stop_rgb(linpat, 1, 0.969, 0.969, 0.969);

    cairo_set_source(cr, linpat);
    cairo_paint(cr);

    int avatar_size = size * 0.5;
    GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), "system-users-symbolic",
                                                        avatar_size, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
    GdkPixbuf *pixbuf_icon = gtk_icon_info_load_icon(icon_info, NULL);
    g_object_unref(icon_info);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    if (pixbuf_icon != NULL) {
        gdk_cairo_set_source_pixbuf(cr, pixbuf_icon, (size - avatar_size) / 2, (size - avatar_size) / 2);
        g_object_unref(pixbuf_icon);
        cairo_rectangle(cr, (size - avatar_size) / 2, (size - avatar_size) / 2, avatar_size, avatar_size);
        cairo_fill(cr);
    }

    GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, size, size);

    /* free resources */
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    return pixbuf;
}

GdkPixbuf *
ring_frame_avatar(GdkPixbuf *avatar) {
134 135 136

    auto w = gdk_pixbuf_get_width(avatar);
    auto h = gdk_pixbuf_get_height(avatar);
137 138 139 140 141 142 143 144
    auto crop_size = std::min(h, w);
    auto new_size = std::max(h, w);
    auto scale = (double)new_size/(double)crop_size;
    GdkPixbuf *crop_avatar = gdk_pixbuf_new (
                               gdk_pixbuf_get_colorspace (avatar),
                               gdk_pixbuf_get_has_alpha (avatar),
                               gdk_pixbuf_get_bits_per_sample (avatar),
                               new_size, new_size);
145
    gdk_pixbuf_scale (avatar, crop_avatar, 0, 0, new_size, new_size,
146
                      (w/2)-(new_size/2), (h/2)-(new_size/2), scale, scale,
147 148 149 150 151
                      GDK_INTERP_BILINEAR);
    auto extra_space = 10;
    auto offset = extra_space/2;
    auto s_surface = new_size + extra_space;
    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, s_surface, s_surface);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
152 153 154
    cairo_t *cr = cairo_create(surface);

    cairo_set_source_rgba(cr, 0, 0, 0, 0);
155
    cairo_rectangle(cr, 0, 0, s_surface, s_surface);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
156 157
    cairo_fill(cr);

158
    double radius = new_size/2;
Stepan Salenikovich's avatar
Stepan Salenikovich committed
159 160
    double degrees = M_PI / 180.0;

161
    // create the square path with ronded corners
Stepan Salenikovich's avatar
Stepan Salenikovich committed
162
    cairo_new_sub_path (cr);
163 164 165
    cairo_arc (cr, offset + new_size - radius, offset + radius, radius, -90 * degrees, 0 * degrees);
    cairo_arc (cr, offset + new_size - radius, offset + new_size - radius, radius, 0 * degrees, 90 * degrees);
    cairo_arc (cr, offset + radius, offset + new_size - radius, radius, 90 * degrees, 180 * degrees);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
166 167 168
    cairo_arc (cr, offset + radius, offset + radius, radius, 180 * degrees, 270 * degrees);
    cairo_close_path (cr);

169
    // in case the image has alpha, we want to first set the background of the part inside the
170
    // blue frame to white; otherwise the resulting image will show whatever is in the background,
171
    // which can be weird in certain cases (eg: the image displayed over a video)
172
    cairo_set_source_rgba(cr, 1, 1, 1, 1);
173 174 175
    cairo_fill_preserve(cr);

    // now draw the image over this black square
176
    gdk_cairo_set_source_pixbuf(cr, crop_avatar, offset, offset);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
177 178
    cairo_fill_preserve(cr);

179
    auto pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, s_surface, s_surface);
Stepan Salenikovich's avatar
Stepan Salenikovich committed
180 181 182 183 184 185

    /* free resources */
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    return pixbuf;
186
}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

static void
create_rounded_rectangle_path(cairo_t *cr, double corner_radius, double x, double y, double w, double h)
{
    double radius = corner_radius;
    double degrees = M_PI / 180.0;

    cairo_new_sub_path (cr);
    cairo_arc (cr, x + w - radius, y + radius, radius, -90 * degrees, 0 * degrees);
    cairo_arc (cr, x + w - radius, y + h - radius, radius, 0 * degrees, 90 * degrees);
    cairo_arc (cr, x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees);
    cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
    cairo_close_path (cr);
}

aviau's avatar
aviau committed
202 203 204 205
/**
 * Draws the presence icon in the top right corner of the given image.
 */
GdkPixbuf *
206 207
ring_draw_status(const GdkPixbuf *avatar, IconStatus status) {
    if (status == IconStatus::INVALID) {
aviau's avatar
aviau committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        // simply return a copy of the original pixbuf
        return gdk_pixbuf_copy(avatar);
    }

    int w = gdk_pixbuf_get_width(avatar);
    int h = gdk_pixbuf_get_height(avatar);
    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
    cairo_t *cr = cairo_create(surface);
    cairo_surface_destroy(surface);

    /* draw original image */
    gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
    cairo_paint(cr);

    /* draw rounded rectangle, with 3 pixel border
     * ie: 6 pixels higher, 6 pixels wider */
    int border_width = 5;
225 226
    double rec_x = w - border_width * 3;
    double rec_y = h - border_width * 3;
aviau's avatar
aviau committed
227 228 229 230 231 232
    double rec_w = border_width * 2;
    double rec_h = border_width * 2;
    double corner_radius = rec_h/2.5;
    create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);

    // For now we don't draw the absent background.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    auto background = RED_BACKGROUND;
    switch(status) {
        case IconStatus::TRYING:
            background = ORANGE_BACKGROUND;
            break;
        case IconStatus::PRESENT:
        case IconStatus::CONNECTED:
            background = GREEN_BACKGROUND;
            break;
        case IconStatus::ABSENT:
        case IconStatus::DISCONNECTED:
        case IconStatus::INVALID:
            background = RED_BACKGROUND;
            break;
    }
aviau's avatar
aviau committed
248 249 250 251 252 253 254
    cairo_set_source_rgba(
        cr,
        background.red,
        background.blue,
        background.green,
        background.alpha
    );
255 256 257 258
    cairo_fill_preserve(cr);
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_set_line_width(cr, 1.2);
    cairo_stroke(cr);
aviau's avatar
aviau committed
259 260 261 262 263 264 265 266 267

    GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);

    /* free resources */
    cairo_destroy(cr);

    return pixbuf;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
/**
 * Draws the unread message count in the bottom right corner of the given image.
 * In the case that the count is less than or equal to 0, nothing is drawn.
 */
GdkPixbuf *
ring_draw_unread_messages(const GdkPixbuf *avatar, int unread_count) {
    if (unread_count <= 0) {
        // simply return a copy of the original pixbuf
        return gdk_pixbuf_copy(avatar);
    }
    int w = gdk_pixbuf_get_width(avatar);
    int h = gdk_pixbuf_get_height(avatar);
    cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
    cairo_t *cr = cairo_create(surface);
    cairo_surface_destroy(surface);

    /* draw original image */
    gdk_cairo_set_source_pixbuf(cr, avatar, 0, 0);
    cairo_paint(cr);

    /* make text */
289
    char *text = g_strdup_printf("%s", unread_count > 9 ? "9+" : std::to_string(unread_count).c_str());
290 291 292 293 294 295 296 297 298 299 300 301 302
    cairo_text_extents_t extents;

    cairo_select_font_face (cr, MSG_COUNT_FONT,
    CAIRO_FONT_SLANT_NORMAL,
    CAIRO_FONT_WEIGHT_NORMAL);

    cairo_set_font_size (cr, MSG_COUNT_FONT_SIZE);
    cairo_text_extents (cr, text, &extents);

    /* draw rounded rectangle around the text, with 3 pixel border
     * ie: 6 pixels higher, 6 pixels wider */
    int border_width = 3;
    double rec_x = w - extents.width - border_width * 2;
303
    double rec_y = 0;
304 305 306 307
    double rec_w = extents.width + border_width * 2;
    double rec_h = extents.height + border_width * 2;
    double corner_radius = rec_h/2.5;
    create_rounded_rectangle_path(cr, corner_radius, rec_x, rec_y, rec_w, rec_h);
308 309 310
    cairo_set_source_rgba(cr,
                          MSG_COUNT_BACKGROUND.red,
                          MSG_COUNT_BACKGROUND.blue, MSG_COUNT_BACKGROUND.green, MSG_COUNT_BACKGROUND.alpha);
311 312 313
    cairo_fill(cr);

    /* draw text */
314
    cairo_move_to (cr, w - extents.width-border_width, extents.height + border_width );
315 316 317 318 319 320 321 322 323 324 325
    cairo_set_source_rgb(cr, MSG_COUNT_FONT_COLOUR.red, MSG_COUNT_FONT_COLOUR.blue, MSG_COUNT_FONT_COLOUR.green);
    cairo_show_text (cr, text);

    GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, w, h);

    /* free resources */
    cairo_destroy(cr);
    g_free(text);

    return pixbuf;
}