naughty: fix margin when using icon
[awesome.git] / draw.c
blob761dd5d57c312750de095638c2bb48b75192f6c3
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <cairo-xcb.h>
24 #include "config.h"
26 #include <langinfo.h>
27 #include <iconv.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <math.h>
32 #include "structs.h"
33 #include "screen.h"
35 #include "common/tokenize.h"
36 #include "common/xutil.h"
38 /** Convert text from any charset to UTF-8 using iconv.
39 * \param iso The ISO string to convert.
40 * \param len The string size.
41 * \param dest The destination pointer. Memory will be allocated, up to you to
42 * free, like any char *.
43 * \param dlen The destination length, can be NULL.
44 * \return True if conversion was done.
46 bool
47 draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen)
49 static iconv_t iso2utf8 = (iconv_t) -1;
50 static int8_t dont_need_convert = -1;
52 if(dont_need_convert == -1)
53 dont_need_convert = !a_strcmp(nl_langinfo(CODESET), "UTF-8");
55 if(!len || dont_need_convert)
56 return false;
58 if(iso2utf8 == (iconv_t) -1)
60 iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
61 if(iso2utf8 == (iconv_t) -1)
63 if(errno == EINVAL)
64 warn("unable to convert text from %s to UTF-8, not available",
65 nl_langinfo(CODESET));
66 else
67 warn("unable to convert text: %s", strerror(errno));
69 return false;
73 size_t orig_utf8len, utf8len;
74 char *utf8;
76 orig_utf8len = utf8len = 2 * len + 1;
77 utf8 = *dest = p_new(char, utf8len);
79 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
81 warn("text conversion failed: %s", strerror(errno));
82 p_delete(dest);
83 return false;
86 if(dlen)
87 *dlen = orig_utf8len - utf8len;
89 return true;
92 /** Create a new Pango font.
93 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
94 * \return A new font.
96 font_t *
97 draw_font_new(const char *fontname)
99 cairo_surface_t *surface;
100 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
101 cairo_t *cr;
102 PangoLayout *layout;
103 font_t *font = p_new(font_t, 1);
105 /* Create a dummy cairo surface, cairo context and pango layout in
106 * order to get font informations */
107 surface = cairo_xcb_surface_create(globalconf.connection,
108 globalconf.default_screen,
109 globalconf.screens.tab[0].visual,
110 s->width_in_pixels,
111 s->height_in_pixels);
113 cr = cairo_create(surface);
114 layout = pango_cairo_create_layout(cr);
116 /* Get the font description used to set text on a PangoLayout */
117 font->desc = pango_font_description_from_string(fontname);
118 pango_layout_set_font_description(layout, font->desc);
120 /* Get height */
121 pango_layout_get_pixel_size(layout, NULL, &font->height);
123 g_object_unref(layout);
124 cairo_destroy(cr);
125 cairo_surface_destroy(surface);
127 return font;
130 /** Delete a font.
131 * \param font Font to delete.
133 void
134 draw_font_delete(font_t **font)
136 if(*font)
138 pango_font_description_free((*font)->desc);
139 p_delete(font);
143 /** Initialize a draw_text_context_t with text data.
144 * \param data The draw text context to init.
145 * \param str The text string to render.
146 * \param slen The text string length.
147 * \return True if everything is ok, false otherwise.
149 bool
150 draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
152 GError *error = NULL;
154 if(!str)
155 return false;
157 if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
159 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
160 if(error)
161 g_error_free(error);
162 return false;
165 data->len = a_strlen(data->text);
167 return true;
170 /** Initialize a new draw context.
171 * \param d The draw context to initialize.
172 * \param phys_screen Physical screen id.
173 * \param width Width.
174 * \param height Height.
175 * \param px Pixmap object to store.
176 * \param fg Foreground color.
177 * \param bg Background color.
179 void
180 draw_context_init(draw_context_t *d, int phys_screen,
181 int width, int height, xcb_pixmap_t px,
182 const xcolor_t *fg, const xcolor_t *bg)
184 d->phys_screen = phys_screen;
185 d->width = width;
186 d->height = height;
187 d->pixmap = px;
188 d->surface = cairo_xcb_surface_create(globalconf.connection,
189 px, globalconf.screens.tab[phys_screen].visual,
190 width, height);
191 d->cr = cairo_create(d->surface);
192 d->layout = pango_cairo_create_layout(d->cr);
193 d->fg = *fg;
194 d->bg = *bg;
197 /** Draw text into a draw context.
198 * \param ctx Draw context to draw to.
199 * \param data Draw text context data.
200 * \param ellip Ellipsize mode.
201 * \param wrap Wrap mode.
202 * \param align Text alignment.
203 * \param valign Vertical text alignment.
204 * \param area Area to draw to.
205 * \param ext Text extents.
207 void
208 draw_text(draw_context_t *ctx, draw_text_context_t *data,
209 PangoEllipsizeMode ellip, PangoWrapMode wrap,
210 alignment_t align, alignment_t valign, area_t area, area_t *ext)
212 pango_layout_set_text(ctx->layout, data->text, data->len);
213 pango_layout_set_width(ctx->layout,
214 pango_units_from_double(area.width));
215 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
216 pango_layout_set_ellipsize(ctx->layout, ellip);
217 pango_layout_set_wrap(ctx->layout, wrap);
218 pango_layout_set_attributes(ctx->layout, data->attr_list);
219 pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
221 /* only honors alignment if enough space */
222 if(ext->width < area.width)
223 switch(align)
225 case AlignCenter:
226 area.x += (area.width - ext->width) / 2;
227 break;
228 case AlignRight:
229 area.x += area.width - ext->width;
230 break;
231 default:
232 break;
235 switch(valign)
237 case AlignCenter:
238 area.y += (area.height - ext->height) / 2;
239 break;
240 case AlignBottom:
241 area.y += area.height - ext->height;
242 break;
243 default:
244 break;
247 cairo_move_to(ctx->cr, area.x, area.y);
249 cairo_set_source_rgba(ctx->cr,
250 ctx->fg.red / 65535.0,
251 ctx->fg.green / 65535.0,
252 ctx->fg.blue / 65535.0,
253 ctx->fg.alpha / 65535.0);
254 pango_cairo_update_layout(ctx->cr, ctx->layout);
255 pango_cairo_show_layout(ctx->cr, ctx->layout);
258 /** Setup color-source for cairo (gradient or mono).
259 * \param ctx Draw context.
260 * \param gradient_vector x, y to x + x_offset, y + y_offset.
261 * \param pcolor Color to use at start of gradient_vector.
262 * \param pcolor_center Color at center of gradient_vector.
263 * \param pcolor_end Color at end of gradient_vector.
264 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
266 static cairo_pattern_t *
267 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
268 const color_t *pcolor, const color_t *pcolor_center,
269 const color_t *pcolor_end)
271 cairo_pattern_t *pat = NULL;
272 bool has_center = pcolor_center->initialized;
273 bool has_end = pcolor_end->initialized;
275 /* no need for a real pattern: */
276 if(!has_end && !has_center)
277 cairo_set_source_rgba(ctx->cr,
278 pcolor->red / 255.0,
279 pcolor->green / 255.0,
280 pcolor->blue / 255.0,
281 pcolor->alpha / 255.0);
282 else
284 pat = cairo_pattern_create_linear(gradient_vector.x,
285 gradient_vector.y,
286 gradient_vector.x + gradient_vector.x_offset,
287 gradient_vector.y + gradient_vector.y_offset);
289 /* pcolor is always set (so far in awesome) */
290 cairo_pattern_add_color_stop_rgba(pat, 0.0,
291 pcolor->red / 255.0,
292 pcolor->green / 255.0,
293 pcolor->blue / 255.0,
294 pcolor->alpha / 255.0);
296 if(has_center)
297 cairo_pattern_add_color_stop_rgba(pat, 0.5,
298 pcolor_center->red / 255.0,
299 pcolor_center->green / 255.0,
300 pcolor_center->blue / 255.0,
301 pcolor_center->alpha / 255.0);
303 if(has_end)
304 cairo_pattern_add_color_stop_rgba(pat, 1.0,
305 pcolor_end->red / 255.0,
306 pcolor_end->green / 255.0,
307 pcolor_end->blue / 255.0,
308 pcolor_end->alpha / 255.0);
309 else
310 cairo_pattern_add_color_stop_rgba(pat, 1.0,
311 pcolor->red / 255.0,
312 pcolor->green / 255.0,
313 pcolor->blue / 255.0,
314 pcolor->alpha / 255.0);
315 cairo_set_source(ctx->cr, pat);
317 return pat;
320 /** Draw rectangle inside the coordinates
321 * \param ctx Draw context
322 * \param geometry geometry
323 * \param line_width line width
324 * \param filled fill rectangle?
325 * \param color color to use
327 void
328 draw_rectangle(draw_context_t *ctx, area_t geometry,
329 float line_width, bool filled, const color_t *color)
331 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
332 cairo_set_line_width(ctx->cr, line_width);
333 cairo_set_miter_limit(ctx->cr, 10.0);
334 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
335 cairo_set_source_rgba(ctx->cr,
336 color->red / 255.0,
337 color->green / 255.0,
338 color->blue / 255.0,
339 color->alpha / 255.0);
340 if(filled)
342 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
343 geometry.width, geometry.height);
344 cairo_fill(ctx->cr);
346 else
348 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
349 geometry.width - line_width, geometry.height - line_width);
350 cairo_stroke(ctx->cr);
354 /** Draw rectangle with gradient colors
355 * \param ctx Draw context.
356 * \param geometry Geometry.
357 * \param line_width Line width.
358 * \param filled Filled rectangle?
359 * \param gradient_vector Color-gradient course.
360 * \param pcolor Color at start of gradient_vector.
361 * \param pcolor_center Color in the center.
362 * \param pcolor_end Color at end of gradient_vector.
364 void
365 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
366 vector_t gradient_vector, const color_t *pcolor,
367 const color_t *pcolor_center, const color_t *pcolor_end)
369 cairo_pattern_t *pat;
371 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
372 cairo_set_line_width(ctx->cr, line_width);
373 cairo_set_miter_limit(ctx->cr, 10.0);
374 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
376 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
378 if(filled)
380 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
381 cairo_fill(ctx->cr);
383 else
385 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
386 cairo_stroke(ctx->cr);
389 if(pat)
390 cairo_pattern_destroy(pat);
393 /** Setup some cairo-things for drawing a graph
394 * \param ctx Draw context
396 void
397 draw_graph_setup(draw_context_t *ctx)
399 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
400 cairo_set_line_width(ctx->cr, 1.0);
401 /* without it, it can draw over the path on sharp angles
402 * ...too long lines * (...graph_line) */
403 cairo_set_miter_limit(ctx->cr, 0.0);
404 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
407 /** Draw a graph.
408 * \param ctx Draw context.
409 * \param rect The area to draw into.
410 * \param from Array of starting-point offsets to draw a graph lines.
411 * \param to Array of end-point offsets to draw a graph lines.
412 * \param cur_index Current position in data-array (cycles around).
413 * \param grow Put new values to the left or to the right.
414 * \param gradient_vector Color-Gradient course.
415 * \param pcolor Color at start of gradient_vector.
416 * \param pcolor_center Color in the center.
417 * \param pcolor_end Color at end of gradient_vector.
419 void
420 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
421 position_t grow, vector_t gradient_vector, const color_t *pcolor,
422 const color_t *pcolor_center, const color_t *pcolor_end)
424 int i = -1;
425 float x = rect.x + 0.5; /* middle of a pixel */
426 cairo_pattern_t *pat;
428 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
429 pcolor, pcolor_center, pcolor_end);
431 if(grow == Right) /* draw from right to left */
433 x += rect.width - 1;
434 while(++i < rect.width)
436 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
437 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
438 x -= 1.0;
440 if(--cur_index < 0)
441 cur_index = rect.width - 1;
444 else /* draw from left to right */
445 while(++i < rect.width)
447 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
448 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
449 x += 1.0;
451 if(--cur_index < 0)
452 cur_index = rect.width - 1;
455 cairo_stroke(ctx->cr);
457 if(pat)
458 cairo_pattern_destroy(pat);
461 /** Draw a line into a graph-widget.
462 * \param ctx Draw context.
463 * \param rect The area to draw into.
464 * \param to array of offsets to draw the line through...
465 * \param cur_index current position in data-array (cycles around)
466 * \param grow put new values to the left or to the right
467 * \param gradient_vector Color-gradient course.
468 * \param pcolor Color at start of gradient_vector.
469 * \param pcolor_center Color in the center.
470 * \param pcolor_end Color at end of gradient_vector.
472 void
473 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
474 position_t grow, vector_t gradient_vector, const color_t *pcolor,
475 const color_t *pcolor_center, const color_t *pcolor_end)
477 int i, w;
478 float x, y;
479 cairo_pattern_t *pat;
481 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
482 * ... it won't fill some pixels! It also looks much nicer so.
483 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
484 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
485 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
486 /* a nicer, better visible line compared to 1.0 */
487 cairo_set_line_width(ctx->cr, 1.25);
489 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
491 /* path through the centers of pixels */
492 x = rect.x + 0.5;
493 y = rect.y + 0.5;
494 w = rect.width;
496 if(grow == Right)
498 /* go through the values from old to new. Begin with the oldest. */
499 if(++cur_index > w - 1)
500 cur_index = 0;
502 cairo_move_to(ctx->cr, x, y - to[cur_index]);
504 else
505 /* on the left border: fills a pixel also when there's only one value */
506 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
508 for(i = 0; i < w; i++)
510 cairo_line_to(ctx->cr, x, y - to[cur_index]);
511 x += 1.0;
513 /* cycles around the index */
514 if(grow == Right)
516 if(++cur_index > w - 1)
517 cur_index = 0;
519 else
521 if(--cur_index < 0)
522 cur_index = w - 1;
526 /* onto the right border: fills a pixel also when there's only one value */
527 if(grow == Right)
528 cairo_line_to(ctx->cr, x, y - to[(cur_index + (w - 1)) % w]);
530 cairo_stroke(ctx->cr);
532 if(pat)
533 cairo_pattern_destroy(pat);
534 /* reset line-width */
535 cairo_set_line_width(ctx->cr, 1.0);
538 /** Draw an image from ARGB data to a draw context.
539 * Data should be stored as an array of alpha, red, blue, green for each pixel
540 * and the array size should be w * h elements long.
541 * \param ctx Draw context to draw to.
542 * \param x X coordinate.
543 * \param y Y coordinate.
544 * \param w Width.
545 * \param h Height.
546 * \param ratio The ratio to apply to the image.
547 * \param data The image pixels array.
549 static void
550 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
551 double ratio, unsigned char *data)
553 cairo_t *cr;
554 cairo_surface_t *source;
556 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
557 #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)
558 sizeof(unsigned char) * 4 * w);
559 #else
560 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
561 #endif
562 cr = cairo_create(ctx->surface);
563 cairo_scale(cr, ratio, ratio);
564 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
566 cairo_paint(cr);
568 cairo_destroy(cr);
569 cairo_surface_destroy(source);
572 /** Draw an image to a draw context.
573 * \param ctx Draw context to draw to.
574 * \param x X coordinate.
575 * \param y Y coordinate.
576 * \param ratio The ratio to apply to the image.
577 * \param image The image to draw.
579 void
580 draw_image(draw_context_t *ctx, int x, int y, double ratio, image_t *image)
582 draw_image_from_argb_data(ctx, x, y, image_getwidth(image), image_getheight(image), ratio, image_getdata(image));
585 /** Rotate a pixmap.
586 * \param ctx Draw context to draw with.
587 * \param src Drawable to draw from.
588 * \param dest Drawable to draw to.
589 * \param src_w Drawable width.
590 * \param src_h Drawable height.
591 * \param dest_w Drawable width.
592 * \param dest_h Drawable height.
593 * \param angle angle to rotate.
594 * \param tx Translate to this x coordinate.
595 * \param ty Translate to this y coordinate.
597 void
598 draw_rotate(draw_context_t *ctx,
599 xcb_pixmap_t src, xcb_pixmap_t dest,
600 int src_w, int src_h,
601 int dest_w, int dest_h,
602 double angle, int tx, int ty)
604 cairo_surface_t *surface, *source;
605 cairo_t *cr;
607 surface = cairo_xcb_surface_create(globalconf.connection, dest,
608 globalconf.screens.tab[ctx->phys_screen].visual,
609 dest_w, dest_h);
610 source = cairo_xcb_surface_create(globalconf.connection, src,
611 globalconf.screens.tab[ctx->phys_screen].visual,
612 src_w, src_h);
613 cr = cairo_create (surface);
615 cairo_translate(cr, tx, ty);
616 cairo_rotate(cr, angle);
618 cairo_set_source_surface(cr, source, 0.0, 0.0);
619 cairo_paint(cr);
621 cairo_destroy(cr);
622 cairo_surface_destroy(source);
623 cairo_surface_destroy(surface);
626 /** Return the width and height of a text in pixel.
627 * \param data The draw context text data.
628 * \return Text height and width.
630 area_t
631 draw_text_extents(draw_text_context_t *data)
633 cairo_surface_t *surface;
634 cairo_t *cr;
635 PangoLayout *layout;
636 PangoRectangle ext;
637 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
638 area_t geom = { 0, 0, 0, 0 };
640 if(data->len <= 0)
641 return geom;
643 surface = cairo_xcb_surface_create(globalconf.connection,
644 globalconf.default_screen,
645 globalconf.screens.tab[0].visual,
646 s->width_in_pixels,
647 s->height_in_pixels);
649 cr = cairo_create(surface);
650 layout = pango_cairo_create_layout(cr);
651 pango_layout_set_text(layout, data->text, data->len);
652 pango_layout_set_attributes(layout, data->attr_list);
653 pango_layout_set_font_description(layout, globalconf.font->desc);
654 pango_layout_get_pixel_extents(layout, NULL, &ext);
655 g_object_unref(layout);
656 cairo_destroy(cr);
657 cairo_surface_destroy(surface);
659 geom.width = ext.width;
660 geom.height = ext.height;
662 return geom;
665 /** Transform a string to a alignment_t type.
666 * Recognized string are flex, fixed, left, center, middle or right.
667 * \param align Atring with align text.
668 * \param len The string length.
669 * \return An alignment_t type.
671 alignment_t
672 draw_align_fromstr(const char *align, ssize_t len)
674 switch(a_tokenize(align, len))
676 case A_TK_CENTER: return AlignCenter;
677 case A_TK_RIGHT: return AlignRight;
678 case A_TK_TOP: return AlignTop;
679 case A_TK_BOTTOM: return AlignBottom;
680 case A_TK_MIDDLE: return AlignMiddle;
681 default: return AlignLeft;
685 /** Transform an alignment to a string.
686 * \param a The alignment.
687 * \return A string which must not be freed.
689 const char *
690 draw_align_tostr(alignment_t a)
692 switch(a)
694 case AlignLeft: return "left";
695 case AlignCenter: return "center";
696 case AlignRight: return "right";
697 case AlignBottom: return "bottom";
698 case AlignTop: return "top";
699 case AlignMiddle: return "middle";
700 default: return NULL;
704 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80