summaryrefslogtreecommitdiffstats
path: root/src/utils/profile-icon.c
blob: 9be296c7b4923e35557bfb94aebed20f64c48c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Profile icon
 *
 * Copyright (C) 2017  Patrick McDermott
 *
 * This file is part of Marquee.
 *
 * Marquee 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.
 *
 * Marquee 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 Marquee.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "profile-icon.h"

#include <string.h>

#include <cairo.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <librsvg/rsvg.h>

#include "svg.h"

#define PROFILE_SVG_WIDTH  16
#define PROFILE_SVG_HEIGHT 16
static const gchar *profile_svg =
	"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" "
		"viewBox=\"0, 0, 16, 16\">\n"
		"<rect x=\"1\" y=\"9\" width=\"14\" height=\"7\" "
			"rx=\"2\" ry=\"2\" fill=\"@COLOR@\" />\n"
		"<ellipse cx=\"8\" cy=\"11\" rx=\"7\" ry=\"4\" "
			"fill=\"@COLOR@\" />\n"
		"<circle cx=\"8\" cy=\"4\" r=\"4\" fill=\"@COLOR@\" />\n"
	"</svg>\n";

gchar *
mq_profile_icon_new(const gchar *color)
{
	if (!mq_svg_is_color_valid(color)) {
		color = "#ff0000";
	}

	return mq_svg_set_color(profile_svg, color);
}

GdkPixbuf *
mq_profile_icon_new_pixbuf(const gchar *color)
{
	cairo_surface_t *surface;
	cairo_t         *cr;
	gchar           *data;
	RsvgHandle      *handle;
	GdkPixbuf       *pixbuf;

	surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
		PROFILE_SVG_WIDTH, PROFILE_SVG_HEIGHT);
	cr = cairo_create(surface);

	data = mq_profile_icon_new(color);
	handle = rsvg_handle_new_from_data((guint8 *) data, strlen(data), NULL);
	rsvg_handle_render_cairo(handle, cr);

	pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0,
		cairo_image_surface_get_width(surface),
		cairo_image_surface_get_height(surface));

	cairo_destroy(cr);
	cairo_surface_destroy(surface);

	return pixbuf;
}