-
Notifications
You must be signed in to change notification settings - Fork 0
/
cairo.cpp
353 lines (314 loc) · 8.49 KB
/
cairo.cpp
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
81
82
83
84
85
86
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-FileCopyrightText: © 2019-2022 Alexandros Theodotou <alex@zrythm.org>
// SPDX-License-Identifier: LicenseRef-ZrythmLicense
#include "cairo.h"
#include "gtk.h"
#include "objects.h"
#include "pango.h"
#include "string.h"
#include "dictionary.h"
GdkPixbuf *
z_gdk_pixbuf_new_from_icon_name (
const char * icon_name,
int width,
int height,
int scale,
GError ** error)
{
GtkIconTheme * icon_theme = z_gtk_icon_theme_get_default ();
GtkIconPaintable * paintable = gtk_icon_theme_lookup_icon (
icon_theme, icon_name, NULL, width, scale, GTK_TEXT_DIR_NONE,
GTK_ICON_LOOKUP_PRELOAD);
GFile * file = gtk_icon_paintable_get_file (paintable);
char * path = g_file_get_path (file);
GdkPixbuf * pixbuf = NULL;
GError * err = NULL;
if (path)
{
pixbuf =
gdk_pixbuf_new_from_file_at_scale (path, width, height, true, &err);
g_free (path);
}
else
{
char * uri = g_file_get_uri (file);
const char * resource_prefix = "resource://";
const char * resource_path = uri + strlen (resource_prefix);
pixbuf = gdk_pixbuf_new_from_resource_at_scale (
resource_path, width, height, true, &err);
g_free (uri);
}
g_object_unref (file);
g_object_unref (paintable);
if (!pixbuf)
{
printf (
"Failed to load pixbuf for icon %s", icon_name);
return NULL;
}
return pixbuf;
}
GdkTexture *
z_gdk_texture_new_from_icon_name (
const char * icon_name,
int width,
int height,
int scale)
{
/* FIXME pass GError and handle gracefully */
GdkPixbuf * pixbuf =
z_gdk_pixbuf_new_from_icon_name (icon_name, width, height, scale, NULL);
g_return_val_if_fail (pixbuf, NULL);
return gdk_texture_new_for_pixbuf (pixbuf);
}
CairoCaches *
CAIRO_CACHES = z_cairo_caches_new () ;
#if 0
void
z_cairo_draw_selection_with_color (
cairo_t * cr,
GdkRGBA * color,
double start_x,
double start_y,
double offset_x,
double offset_y)
{
cairo_set_line_width (cr, 1);
cairo_set_source_rgba (
cr, color->red, color->green, color->blue,
color->alpha);
cairo_rectangle (
cr, start_x, start_y, offset_x, offset_y);
cairo_stroke (cr);
cairo_set_source_rgba (
cr, color->red / 3.0, color->green / 3.0,
color->blue / 3.0, color->alpha / 3.0);
cairo_rectangle (
cr, start_x, start_y, offset_x, offset_y);
cairo_fill (cr);
}
void
z_cairo_draw_selection (
cairo_t * cr,
double start_x,
double start_y,
double offset_x,
double offset_y)
{
GdkRGBA color =
Z_GDK_RGBA_INIT (0.9, 0.9, 0.9, 1.0);
z_cairo_draw_selection_with_color (
cr, &color, start_x, start_y, offset_x,
offset_y);
}
#endif
void
z_cairo_draw_horizontal_line (
cairo_t * cr,
double y,
double from_x,
double to_x,
double line_width,
double alpha)
{
cairo_set_source_rgba (cr, 0.7, 0.7, 0.7, alpha);
cairo_rectangle (cr, from_x, y, to_x - from_x, line_width);
cairo_fill (cr);
}
void
z_cairo_draw_vertical_line (
cairo_t * cr,
double x,
double from_y,
double to_y,
double line_width)
{
cairo_rectangle (cr, x, from_y, line_width, to_y - from_y);
cairo_fill (cr);
}
/**
* Creates a PangoLayout with default settings.
*/
PangoLayout *
z_cairo_create_default_pango_layout (GtkWidget * widget)
{
PangoLayout * layout = gtk_widget_create_pango_layout (widget, NULL);
PangoFontDescription * desc =
pango_font_description_from_string (Z_CAIRO_FONT);
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
return layout;
}
/**
* Creates a PangoLayout to be cached in widgets
* based on the given settings.
*/
PangoLayout *
z_cairo_create_pango_layout_from_description (
GtkWidget * widget,
PangoFontDescription * descr,
PangoEllipsizeMode ellipsize_mode,
int ellipsize_padding)
{
PangoLayout * layout = z_pango_create_layout_from_description (widget, descr);
if (ellipsize_mode > PANGO_ELLIPSIZE_NONE)
{
pango_layout_set_width (
layout,
pango_units_from_double (
MAX (gtk_widget_get_width (widget) - ellipsize_padding * 2, 1)));
pango_layout_set_ellipsize (layout, ellipsize_mode);
}
return layout;
}
/**
* Creates a PangoLayout to be cached in widgets
* based on the given settings.
*/
PangoLayout *
z_cairo_create_pango_layout_from_string (
GtkWidget * widget,
const char * font,
PangoEllipsizeMode ellipsize_mode,
int ellipsize_padding)
{
PangoFontDescription * desc = pango_font_description_from_string (font);
PangoLayout * layout = z_cairo_create_pango_layout_from_description (
widget, desc, ellipsize_mode, ellipsize_padding);
pango_font_description_free (desc);
return layout;
}
/**
* Gets the width of the given text in pixels
* for the given widget.
*
* Assumes that the layout is already set on
* the widget.
*
* @param widget The widget to derive a PangoLayout
* from.
* @param text The text to draw.
* @param width The width to fill in.
* @param height The height to fill in.
*/
void
_z_cairo_get_text_extents_for_widget (
GtkWidget * widget,
PangoLayout * layout,
const char * text,
int * width,
int * height)
{
g_return_if_fail (layout && widget && text);
pango_layout_set_markup (layout, text, -1);
pango_layout_get_pixel_size (layout, width, height);
}
/**
* Draws the given text using the given font
* starting at the given position.
*/
void
z_cairo_draw_text_full (
cairo_t * cr,
GtkWidget * widget,
PangoLayout * layout,
const char * text,
int start_x,
int start_y)
{
g_return_if_fail (cr && layout && widget && text);
cairo_translate (cr, start_x, start_y);
pango_layout_set_markup (layout, text, -1);
pango_cairo_show_layout (cr, layout);
cairo_translate (cr, -start_x, -start_y);
}
/**
* Returns a surface for the icon name.
*/
cairo_surface_t *
z_cairo_get_surface_from_icon_name (const char * icon_name, int size, int scale)
{
g_return_val_if_fail (icon_name, NULL);
cairo_surface_t * surface = dictionary_find_simple (
CAIRO_CACHES->icon_surface_dict, icon_name, cairo_surface_t);
if (!surface)
{
GdkTexture * texture =
z_gdk_texture_new_from_icon_name (icon_name, size, size, scale);
if (!texture)
{
g_message ("failed to load texture from icon %s", icon_name);
/* return a warning icon if not found */
if (string_is_equal (icon_name, "data-warning"))
{
return NULL;
}
else
{
return z_cairo_get_surface_from_icon_name (
"data-warning", size, scale);
}
}
surface = cairo_image_surface_create (
CAIRO_FORMAT_ARGB32, gdk_texture_get_width (texture),
gdk_texture_get_height (texture));
gdk_texture_download (
texture, cairo_image_surface_get_data (surface),
(gsize) cairo_image_surface_get_stride (surface));
cairo_surface_mark_dirty (surface);
g_object_unref (texture);
dictionary_add (CAIRO_CACHES->icon_surface_dict, icon_name, surface);
}
return surface;
}
/**
* Resets a surface and cairo_t with a new surface
* and cairo_t based on the given rectangle and
* cairo_t.
*
* To be used inside draw calls of widgets that
* use caching.
*
* @param width New surface width.
* @param height New surface height.
*/
void
z_cairo_reset_caches (
cairo_t ** cr_cache,
cairo_surface_t ** surface_cache,
int width,
int height,
cairo_t * new_cr)
{
g_return_if_fail (width < 8000 && height < 8000);
if (*surface_cache)
{
cairo_surface_destroy (*surface_cache);
}
if (*cr_cache)
{
cairo_destroy (*cr_cache);
}
*surface_cache = cairo_surface_create_similar (
cairo_get_target (new_cr), CAIRO_CONTENT_COLOR_ALPHA, width, height);
*cr_cache = cairo_create (*surface_cache);
}
CairoCaches *
z_cairo_caches_new (void)
{
CairoCaches * self = object_new (CairoCaches);
self->icon_surface_dict = dictionary_new ();
return self;
}
void
z_cairo_caches_free (CairoCaches * self)
{
object_free_w_func_and_null (dictionary_free, self->icon_surface_dict);
object_zero_and_free (self);
}
GtkIconTheme *
z_gtk_icon_theme_get_default (void)
{
GdkDisplay * display = gdk_display_get_default ();
GtkIconTheme * icon_theme = gtk_icon_theme_get_for_display (display);
return icon_theme;
}