forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.c
More file actions
371 lines (332 loc) · 10.8 KB
/
Copy pathdraw.c
File metadata and controls
371 lines (332 loc) · 10.8 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* draw.c - draw functions
*
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <cairo-xcb.h>
#include "config.h"
#include <langinfo.h>
#include <iconv.h>
#include <errno.h>
#include <ctype.h>
#include "draw.h"
#include "awesome.h"
#include "screen.h"
#include "font.h"
#include "common/tokenize.h"
#include "common/xutil.h"
/** Convert text from any charset to UTF-8 using iconv.
* \param iso The ISO string to convert.
* \param len The string size.
* \param dest The destination pointer. Memory will be allocated, up to you to
* free, like any char *.
* \param dlen The destination length, can be NULL.
* \return True if conversion was done.
*/
bool
draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen)
{
static iconv_t iso2utf8 = (iconv_t) -1;
static int8_t dont_need_convert = -1;
if(dont_need_convert == -1)
dont_need_convert = !a_strcmp(nl_langinfo(CODESET), "UTF-8");
if(!len || dont_need_convert)
return false;
if(iso2utf8 == (iconv_t) -1)
{
iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
if(iso2utf8 == (iconv_t) -1)
{
if(errno == EINVAL)
warn("unable to convert text from %s to UTF-8, not available",
nl_langinfo(CODESET));
else
warn("unable to convert text: %s", strerror(errno));
return false;
}
}
size_t orig_utf8len, utf8len;
char *utf8;
orig_utf8len = utf8len = 2 * len + 1;
utf8 = *dest = p_new(char, utf8len);
if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
{
warn("text conversion failed: %s", strerror(errno));
p_delete(dest);
return false;
}
if(dlen)
*dlen = orig_utf8len - utf8len;
return true;
}
/** Initialize a draw_text_context_t with text data.
* \param data The draw text context to init.
* \param str The text string to render.
* \param slen The text string length.
* \return True if everything is ok, false otherwise.
*/
bool
draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
{
GError *error = NULL;
if(!str)
return false;
if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
{
warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
if(error)
g_error_free(error);
return false;
}
data->len = a_strlen(data->text);
return true;
}
/** Initialize a new draw context.
* \param d The draw context to initialize.
* \param pixmap The pixmap to draw onto.
* \param width Width.
* \param height Height.
*/
#include "awesome.h"
void
draw_context_init(draw_context_t *d,
xcb_pixmap_t pixmap,
int width, int height)
{
d->surface = cairo_xcb_surface_create(_G_connection,
pixmap, _G_visual,
width, height);
d->cr = cairo_create(d->surface);
d->layout = pango_cairo_create_layout(d->cr);
};
/** Wipe a draw context.
* \param ctx The draw_context_t to wipe.
*/
void
draw_context_wipe(draw_context_t *ctx)
{
if(ctx->layout)
g_object_unref(ctx->layout);
if(ctx->surface)
cairo_surface_destroy(ctx->surface);
if(ctx->cr)
cairo_destroy(ctx->cr);
}
/** Compute coordinates of an object of width/height on an area, with alignment
* and vertical alignment
* \param area The area to place the object on.
* \param height The height of the object.
* \param width The width of the object.
* \param align Horizontal alignment.
* \param valign Vertical alignment.
* \return An area with x and y set to coordinates.
*/
static area_t
draw_align_compute(area_t area, int width, int height, alignment_t align, alignment_t valign)
{
switch(align)
{
case AlignCenter:
area.x += (area.width - width) / 2;
break;
case AlignRight:
area.x += area.width - width;
break;
default:
break;
}
switch(valign)
{
case AlignCenter:
area.y += (area.height - height) / 2;
break;
case AlignBottom:
area.y += area.height - height;
break;
default:
break;
}
return area;
}
/** Draw text into a draw context.
* \param ctx Draw context to draw to.
* \param data Draw text context data.
* \param fg The foreground color used to draw the text.
* \param area Area to draw to.
*/
void
draw_text(draw_context_t *ctx, draw_text_context_t *data, const color_t *fg, area_t area)
{
pango_layout_set_text(ctx->layout, data->text, data->len);
pango_layout_set_width(ctx->layout,
pango_units_from_double(area.width));
pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
pango_layout_set_ellipsize(ctx->layout, data->ellip);
pango_layout_set_wrap(ctx->layout, data->wrap);
pango_layout_set_attributes(ctx->layout, data->attr_list);
pango_layout_set_font_description(ctx->layout, _G_font.desc);
PangoRectangle ext;
pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
area = draw_align_compute(area, ext.width, ext.height, data->align, data->valign);
cairo_move_to(ctx->cr, area.x, area.y);
cairo_set_source_rgba(ctx->cr,
fg->red / 255.0,
fg->green / 255.0,
fg->blue / 255.0,
fg->alpha / 255.0);
pango_cairo_update_layout(ctx->cr, ctx->layout);
pango_cairo_show_layout(ctx->cr, ctx->layout);
}
/** Draw rectangle inside the coordinates
* \param ctx Draw context
* \param geometry geometry
* \param line_width line width
* \param filled fill rectangle?
* \param color color to use
*/
void
draw_rectangle(draw_context_t *ctx, area_t geometry,
float line_width, bool filled, const color_t *color)
{
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
cairo_set_line_width(ctx->cr, line_width);
cairo_set_miter_limit(ctx->cr, 10.0);
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
cairo_set_source_rgba(ctx->cr,
color->red / 255.0,
color->green / 255.0,
color->blue / 255.0,
color->alpha / 255.0);
if(filled)
{
cairo_rectangle(ctx->cr, geometry.x, geometry.y,
geometry.width, geometry.height);
cairo_fill(ctx->cr);
}
else
{
cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
geometry.width - line_width, geometry.height - line_width);
cairo_stroke(ctx->cr);
}
}
/** Draw an image from ARGB data to a draw context.
* Data should be stored as an array of alpha, red, blue, green for each pixel
* and the array size should be w * h elements long.
* \param ctx Draw context to draw to.
* \param x X coordinate.
* \param y Y coordinate.
* \param w Width.
* \param h Height.
* \param data The image pixels array.
*/
static void
draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h, unsigned char *data)
{
cairo_surface_t *source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
#if CAIRO_VERSION_MAJOR < 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR < 5) || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR == 5 && CAIRO_VERSION_MICRO < 8)
sizeof(unsigned char) * 4 * w);
#else
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
#endif
cairo_t *cr = cairo_create(ctx->surface);
cairo_set_source_surface(cr, source, x, y);
cairo_paint(cr);
cairo_destroy(cr);
cairo_surface_destroy(source);
}
/** Draw an image to a draw context.
* \param ctx Draw context to draw to.
* \param area The area to draw on.
* \param align The horizonal alignment in the above area.
* \param valign The vertical alignment.
* \param image The image to draw.
*/
void
draw_image(draw_context_t *ctx, area_t area, alignment_t align, alignment_t valign, image_t *image)
{
if(image)
{
int width = image_get_width(image);
int height = image_get_height(image);
area = draw_align_compute(area, width, height, align, valign);
draw_image_from_argb_data(ctx, area.x, area.y, width, height, image_get_data(image));
}
}
/** Transform a string to a alignment_t type.
* Recognized string are flex, fixed, left, center, middle or right.
* \param align A string with align text.
* \param len The string length.
* \return An alignment_t type.
*/
alignment_t
draw_align_fromstr(const char *align, ssize_t len)
{
switch(a_tokenize(align, len))
{
case A_TK_CENTER: return AlignCenter;
case A_TK_RIGHT: return AlignRight;
default: return AlignLeft;
}
}
/** Transform an alignment to a string.
* \param a The alignment.
* \return A string which must not be freed.
*/
const char *
draw_align_tostr(alignment_t a)
{
switch(a)
{
case AlignLeft: return "left";
case AlignCenter: return "center";
case AlignRight: return "right";
default: return NULL;
}
}
/** Transform a string to a alignment_t type.
* Recognized string are flex, fixed, left, center, middle or right.
* \param align A string with align text.
* \param len The string length.
* \return An alignment_t type.
*/
alignment_t
draw_valign_fromstr(const char *align, ssize_t len)
{
switch(a_tokenize(align, len))
{
case A_TK_CENTER: return AlignCenter;
case A_TK_BOTTOM: return AlignBottom;
default: return AlignTop;
}
}
/** Transform an alignment to a string.
* \param a The alignment.
* \return A string which must not be freed.
*/
const char *
draw_valign_tostr(alignment_t a)
{
switch(a)
{
case AlignCenter: return "center";
case AlignBottom: return "bottom";
case AlignTop: return "top";
default: return NULL;
}
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80