draw: stop parsing Pango markup twice, store AttrList
[awesome.git] / common / draw.c
blob3b62cd71977cc21599a4e8a16d6a84da2282d1d5
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007-2008 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"
25 #ifdef WITH_IMLIB2
26 #include <Imlib2.h>
27 #else
28 #include <gdk/gdkcairo.h>
29 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #endif
32 #include <xcb/xcb.h>
33 #include <xcb/xcb_aux.h>
35 #include <langinfo.h>
36 #include <iconv.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <math.h>
42 #include "common/tokenize.h"
43 #include "common/draw.h"
44 #include "common/markup.h"
45 #include "common/xutil.h"
47 void
48 draw_parser_data_init(draw_parser_data_t *pdata)
50 p_clear(pdata, 1);
53 void
54 draw_parser_data_wipe(draw_parser_data_t *pdata)
56 if(pdata)
57 draw_image_delete(&pdata->bg_image);
60 static iconv_t iso2utf8 = (iconv_t) -1;
62 /** Convert text from any charset to UTF-8 using iconv.
63 * \param iso The ISO string to convert.
64 * \param len The string size.
65 * \return NULL if error, otherwise pointer to the new converted string.
67 char *
68 draw_iso2utf8(const char *iso, size_t len)
70 size_t utf8len;
71 char *utf8, *utf8p;
73 if(!len)
74 return NULL;
76 if(!a_strcmp(nl_langinfo(CODESET), "UTF-8"))
77 return NULL;
79 if(iso2utf8 == (iconv_t) -1)
81 iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
82 if(iso2utf8 == (iconv_t) -1)
84 if(errno == EINVAL)
85 warn("unable to convert text from %s to UTF-8, not available",
86 nl_langinfo(CODESET));
87 else
88 warn("unable to convert text: %s", strerror(errno));
90 return NULL;
94 utf8len = 2 * len + 1;
95 utf8 = utf8p = p_new(char, utf8len);
97 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
99 warn("text conversion failed: %s", strerror(errno));
100 p_delete(&utf8p);
103 return utf8p;
106 static xcb_visualtype_t *
107 draw_screen_default_visual(xcb_screen_t *s)
109 xcb_depth_iterator_t depth_iter;
110 xcb_visualtype_iterator_t visual_iter;
112 if(!s)
113 return NULL;
115 for(depth_iter = xcb_screen_allowed_depths_iterator(s);
116 depth_iter.rem; xcb_depth_next (&depth_iter))
117 for(visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
118 visual_iter.rem; xcb_visualtype_next (&visual_iter))
119 if (s->root_visual == visual_iter.data->visual_id)
120 return visual_iter.data;
122 return NULL;
125 /** Create a new draw context.
126 * \param conn Connection ref.
127 * \param phys_screen Physical screen id.
128 * \param width Width.
129 * \param height Height.
130 * \param px Pixmap object to store.
131 * \param fg Foreground color.
132 * \param bg Background color.
133 * \return A draw context pointer.
135 draw_context_t *
136 draw_context_new(xcb_connection_t *conn, int phys_screen,
137 int width, int height, xcb_pixmap_t px,
138 const xcolor_t *fg, const xcolor_t *bg)
140 draw_context_t *d = p_new(draw_context_t, 1);
141 xcb_screen_t *s = xutil_screen_get(conn, phys_screen);
143 d->connection = conn;
144 d->phys_screen = phys_screen;
145 d->width = width;
146 d->height = height;
147 d->depth = s->root_depth;
148 d->visual = draw_screen_default_visual(s);
149 d->pixmap = px;
150 d->surface = cairo_xcb_surface_create(conn, px, d->visual, width, height);
151 d->cr = cairo_create(d->surface);
152 d->layout = pango_cairo_create_layout(d->cr);
153 d->fg = *fg;
154 d->bg = *bg;
156 return d;
159 /** Create a new Pango font
160 * \param conn Connection ref
161 * \param phys_screen The physical screen number.
162 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE])
163 * \return a new font
165 font_t *
166 draw_font_new(xcb_connection_t *conn, int phys_screen, const char *fontname)
168 cairo_surface_t *surface;
169 xcb_screen_t *s = xutil_screen_get(conn, phys_screen);
170 cairo_t *cr;
171 PangoLayout *layout;
172 font_t *font = p_new(font_t, 1);
174 /* Create a dummy cairo surface, cairo context and pango layout in
175 * order to get font informations */
176 surface = cairo_xcb_surface_create(conn,
177 phys_screen,
178 draw_screen_default_visual(s),
179 s->width_in_pixels,
180 s->height_in_pixels);
182 cr = cairo_create(surface);
183 layout = pango_cairo_create_layout(cr);
185 /* Get the font description used to set text on a PangoLayout */
186 font->desc = pango_font_description_from_string(fontname);
187 pango_layout_set_font_description(layout, font->desc);
189 /* Get height */
190 pango_layout_get_pixel_size(layout, NULL, &font->height);
192 g_object_unref(layout);
193 cairo_destroy(cr);
194 cairo_surface_destroy(surface);
196 return font;
199 /** Delete a font.
200 * \param font Font to delete.
202 void
203 draw_font_delete(font_t **font)
205 if(*font)
207 pango_font_description_free((*font)->desc);
208 p_delete(font);
212 static void
213 draw_markup_on_element(markup_parser_data_t *p, const char *elem,
214 const char **names, const char **values)
216 draw_parser_data_t *data = p->priv;
218 xcolor_init_request_t reqs[3];
219 int8_t i, bg_color_nbr = -1, reqs_nbr = -1;
221 /* hack: markup.c validates tags so we can avoid strcmps here */
222 switch (*elem) {
223 case 'b':
224 if(elem[1] == 'g') /* bg */
225 for(; *names; names++, values++)
226 switch(a_tokenize(*names, -1))
228 case A_TK_COLOR:
229 reqs[++reqs_nbr] = xcolor_init_unchecked(data->connection,
230 &data->bg_color,
231 data->phys_screen,
232 *values,
233 a_strlen(*values));
235 bg_color_nbr = reqs_nbr;
236 break;
237 case A_TK_IMAGE:
238 if(data->bg_image)
239 draw_image_delete(&data->bg_image);
240 data->bg_image = draw_image_new(*values);
241 break;
242 case A_TK_ALIGN:
243 data->bg_align = draw_align_fromstr(*values, -1);
244 break;
245 case A_TK_RESIZE:
246 data->bg_resize = a_strtobool(*values, -1);
247 default:
248 break;
250 else /* border */
251 for(; *names; names++, values++)
252 switch(a_tokenize(*names, -1))
254 case A_TK_COLOR:
255 reqs[++reqs_nbr] = xcolor_init_unchecked(data->connection,
256 &data->border.color,
257 data->phys_screen,
258 *values,
259 a_strlen(*values));
260 break;
261 case A_TK_WIDTH:
262 data->border.width = atoi(*values);
263 break;
264 default:
265 break;
267 break;
268 case 't': /* text */
269 for(; *names; names++, values++)
270 switch(a_tokenize(*names, -1))
272 case A_TK_ALIGN:
273 data->align = draw_align_fromstr(*values, -1);
274 break;
275 case A_TK_SHADOW:
276 reqs[++reqs_nbr] = xcolor_init_unchecked(data->connection,
277 &data->shadow.color,
278 data->phys_screen,
279 *values,
280 a_strlen(*values));
281 break;
282 case A_TK_SHADOW_OFFSET:
283 data->shadow.offset = atoi(*values);
284 break;
285 default:
286 break;
288 break;
289 case 'm': /* margin */
290 for (; *names; names++, values++)
291 switch(a_tokenize(*names, -1))
293 case A_TK_LEFT:
294 data->margin.left = atoi(*values);
295 break;
296 case A_TK_RIGHT:
297 data->margin.right = atoi(*values);
298 break;
299 default:
300 break;
302 break;
305 for(i = 0; i <= reqs_nbr; i++)
306 if(i == bg_color_nbr)
307 data->has_bg_color = xcolor_init_reply(data->connection, reqs[i]);
308 else
309 xcolor_init_reply(data->connection, reqs[i]);
312 bool
313 draw_text_markup_expand(draw_parser_data_t *data,
314 const char *str, ssize_t slen)
316 static char const * const elements[] = { "bg", "text", "margin", "border", NULL };
317 markup_parser_data_t p =
319 .elements = elements,
320 .priv = data,
321 .on_element = &draw_markup_on_element,
323 char *text = NULL;
324 GError *error = NULL;
325 bool ret = false;
327 markup_parser_data_init(&p);
329 if(!markup_parse(&p, str, slen))
330 goto bailout;
332 if(!pango_parse_markup(p.text.s, p.text.len, 0, &data->attr_list, &text, NULL, &error))
333 goto bailout;
335 /* stole text */
336 data->text = text;
337 data->len = a_strlen(text);
338 ret = true;
340 bailout:
341 markup_parser_data_wipe(&p);
342 return ret;
345 /** Draw text into a draw context.
346 * \param ctx Draw context to draw to.
347 * \param font The font to use.
348 * \param area Area to draw to.
349 * \param text Text to draw.
350 * \param len Text to draw length.
351 * \param data Optional parser data.
353 void
354 draw_text(draw_context_t *ctx, font_t *font,
355 area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata)
357 int x, y;
358 PangoRectangle ext;
359 draw_parser_data_t parser_data;
361 if(!pdata)
363 draw_parser_data_init(&parser_data);
364 parser_data.connection = ctx->connection;
365 parser_data.phys_screen = ctx->phys_screen;
366 if(draw_text_markup_expand(&parser_data, text, len))
368 text = parser_data.text;
369 len = parser_data.len;
371 pdata = &parser_data;
373 else
375 text = pdata->text;
376 len = pdata->len;
379 if(pdata->has_bg_color)
380 draw_rectangle(ctx, area, 1.0, true, &pdata->bg_color);
382 if(pdata->border.width > 0)
383 draw_rectangle(ctx, area, pdata->border.width, false, &pdata->border.color);
385 if(pdata->bg_image)
387 x = area.x;
388 y = area.y;
389 switch(pdata->bg_align)
391 case AlignCenter:
392 x += (area.width - pdata->bg_image->width) / 2;
393 break;
394 case AlignRight:
395 x += area.width- pdata->bg_image->width;
396 break;
397 default:
398 break;
400 draw_image(ctx, x, y, pdata->bg_resize ? area.height : 0, pdata->bg_image);
403 pango_layout_set_text(ctx->layout, pdata->text, pdata->len);
404 pango_layout_set_width(ctx->layout,
405 pango_units_from_double(area.width
406 - (pdata->margin.left
407 + pdata->margin.right)));
408 pango_layout_set_ellipsize(ctx->layout, PANGO_ELLIPSIZE_END);
409 pango_layout_set_attributes(ctx->layout, pdata->attr_list);
410 pango_layout_set_font_description(ctx->layout, font->desc);
411 pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
413 x = area.x + pdata->margin.left;
414 /* + 1 is added for rounding, so that in any case of doubt we rather draw
415 * the text 1px lower than too high which usually results in a better type
416 * face */
417 y = area.y + (ctx->height - font->height + 1) / 2;
419 switch(pdata->align)
421 case AlignCenter:
422 x += (area.width - ext.width) / 2;
423 break;
424 case AlignRight:
425 x += area.width - ext.width;
426 break;
427 default:
428 break;
431 if(pdata->shadow.offset)
433 cairo_set_source_rgba(ctx->cr,
434 pdata->shadow.color.red / 65535.0,
435 pdata->shadow.color.green / 65535.0,
436 pdata->shadow.color.blue / 65535.0,
437 pdata->shadow.color.alpha / 65535.0);
438 cairo_move_to(ctx->cr, x + pdata->shadow.offset, y + pdata->shadow.offset);
439 pango_cairo_layout_path(ctx->cr, ctx->layout);
440 cairo_stroke(ctx->cr);
443 cairo_move_to(ctx->cr, x, y);
445 cairo_set_source_rgba(ctx->cr,
446 ctx->fg.red / 65535.0,
447 ctx->fg.green / 65535.0,
448 ctx->fg.blue / 65535.0,
449 ctx->fg.alpha / 65535.0);
450 pango_cairo_update_layout(ctx->cr, ctx->layout);
451 pango_cairo_show_layout(ctx->cr, ctx->layout);
453 if (pdata == &parser_data)
454 draw_parser_data_wipe(&parser_data);
457 /** Setup color-source for cairo (gradient or mono).
458 * \param ctx Draw context.
459 * \param gradient_vector x, y to x + x_offset, y + y_offset.
460 * \param pcolor Color to use at start of gradient_vector.
461 * \param pcolor_center Color at center of gradient_vector.
462 * \param pcolor_end Color at end of gradient_vector.
463 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
465 static cairo_pattern_t *
466 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
467 const xcolor_t *pcolor, const xcolor_t *pcolor_center,
468 const xcolor_t *pcolor_end)
470 cairo_pattern_t *pat = NULL;
471 bool has_center = pcolor_center->initialized;
472 bool has_end = pcolor_end->initialized;
474 /* no need for a real pattern: */
475 if(!has_end && !has_center)
476 cairo_set_source_rgba(ctx->cr,
477 pcolor->red / 65535.0,
478 pcolor->green / 65535.0,
479 pcolor->blue / 65535.0,
480 pcolor->alpha / 65535.0);
481 else
483 pat = cairo_pattern_create_linear(gradient_vector.x,
484 gradient_vector.y,
485 gradient_vector.x + gradient_vector.x_offset,
486 gradient_vector.y + gradient_vector.y_offset);
488 /* pcolor is always set (so far in awesome) */
489 cairo_pattern_add_color_stop_rgba(pat, 0.0,
490 pcolor->red / 65535.0,
491 pcolor->green / 65535.0,
492 pcolor->blue / 65535.0,
493 pcolor->alpha / 65535.0);
495 if(has_center)
496 cairo_pattern_add_color_stop_rgba(pat, 0.5,
497 pcolor_center->red / 65535.0,
498 pcolor_center->green / 65535.0,
499 pcolor_center->blue / 65535.0,
500 pcolor_center->alpha / 65535.0);
502 if(has_end)
503 cairo_pattern_add_color_stop_rgba(pat, 1.0,
504 pcolor_end->red / 65535.0,
505 pcolor_end->green / 65535.0,
506 pcolor_end->blue / 65535.0,
507 pcolor_end->alpha / 65535.0);
508 else
509 cairo_pattern_add_color_stop_rgba(pat, 1.0,
510 pcolor->red / 65535.0,
511 pcolor->green / 65535.0,
512 pcolor->blue / 65535.0,
513 pcolor->alpha / 65535.0);
514 cairo_set_source(ctx->cr, pat);
516 return pat;
519 /** Draw rectangle inside the coordinates
520 * \param ctx Draw context
521 * \param geometry geometry
522 * \param line_width line width
523 * \param filled fill rectangle?
524 * \param color color to use
526 void
527 draw_rectangle(draw_context_t *ctx, area_t geometry,
528 float line_width, bool filled, const xcolor_t *color)
530 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
531 cairo_set_line_width(ctx->cr, line_width);
532 cairo_set_miter_limit(ctx->cr, 10.0);
533 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
534 cairo_set_source_rgba(ctx->cr,
535 color->red / 65535.0,
536 color->green / 65535.0,
537 color->blue / 65535.0,
538 color->alpha / 65535.0);
539 if(filled)
541 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
542 geometry.width, geometry.height);
543 cairo_fill(ctx->cr);
545 else
547 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
548 geometry.width - line_width, geometry.height - line_width);
549 cairo_stroke(ctx->cr);
553 /** Draw rectangle with gradient colors
554 * \param ctx Draw context.
555 * \param geometry Geometry.
556 * \param line_width Line width.
557 * \param filled Filled rectangle?
558 * \param gradient_vector Color-gradient course.
559 * \param pcolor Color at start of gradient_vector.
560 * \param pcolor_center Color in the center.
561 * \param pcolor_end Color at end of gradient_vector.
563 void
564 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
565 vector_t gradient_vector, const xcolor_t *pcolor,
566 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
568 cairo_pattern_t *pat;
570 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
571 cairo_set_line_width(ctx->cr, line_width);
572 cairo_set_miter_limit(ctx->cr, 10.0);
573 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
575 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
577 if(filled)
579 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
580 cairo_fill(ctx->cr);
582 else
584 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
585 cairo_stroke(ctx->cr);
588 if(pat)
589 cairo_pattern_destroy(pat);
592 /** Setup some cairo-things for drawing a graph
593 * \param ctx Draw context
595 void
596 draw_graph_setup(draw_context_t *ctx)
598 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
599 cairo_set_line_width(ctx->cr, 1.0);
600 /* without it, it can draw over the path on sharp angles
601 * ...too long lines * (...graph_line) */
602 cairo_set_miter_limit(ctx->cr, 0.0);
603 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
606 /** Draw a graph.
607 * \param ctx Draw context.
608 * \param rect The area to draw into.
609 * \param from Array of starting-point offsets to draw a graph lines.
610 * \param to Array of end-point offsets to draw a graph lines.
611 * \param cur_index Current position in data-array (cycles around).
612 * \param grow Put new values to the left or to the right.
613 * \param gradient_vector Color-Gradient course.
614 * \param pcolor Color at start of gradient_vector.
615 * \param pcolor_center Color in the center.
616 * \param pcolor_end Color at end of gradient_vector.
618 void
619 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
620 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
621 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
623 int i = -1;
624 float x = rect.x + 0.5; /* middle of a pixel */
625 cairo_pattern_t *pat;
627 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
628 pcolor, pcolor_center, pcolor_end);
630 if(grow == Right) /* draw from right to left */
632 x += rect.width - 1;
633 while(++i < rect.width)
635 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
636 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
637 x -= 1.0;
639 if (--cur_index < 0)
640 cur_index = rect.width - 1;
643 else /* draw from left to right */
644 while(++i < rect.width)
646 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
647 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
648 x += 1.0;
650 if (--cur_index < 0)
651 cur_index = rect.width - 1;
654 cairo_stroke(ctx->cr);
656 if(pat)
657 cairo_pattern_destroy(pat);
660 /** Draw a line into a graph-widget.
661 * \param ctx Draw context.
662 * \param rect The area to draw into.
663 * \param to array of offsets to draw the line through...
664 * \param cur_index current position in data-array (cycles around)
665 * \param grow put new values to the left or to the right
666 * \param gradient_vector Color-gradient course.
667 * \param pcolor Color at start of gradient_vector.
668 * \param pcolor_center Color in the center.
669 * \param pcolor_end Color at end of gradient_vector.
671 void
672 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
673 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
674 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
676 int i, w;
677 float x, y;
678 cairo_pattern_t *pat;
680 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
681 * ... it won't fill some pixels! It also looks much nicer so.
682 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
683 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
684 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
685 /* a nicer, better visible line compared to 1.0 */
686 cairo_set_line_width(ctx->cr, 1.25);
688 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
690 /* path through the centers of pixels */
691 x = rect.x + 0.5;
692 y = rect.y + 0.5;
693 w = rect.width;
695 if(grow == Right)
697 /* go through the values from old to new. Begin with the oldest. */
698 if(++cur_index > w - 1)
699 cur_index = 0;
701 cairo_move_to(ctx->cr, x, y - to[cur_index]);
703 else
704 /* on the left border: fills a pixel also when there's only one value */
705 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
707 for(i = 0; i < w; i++)
709 cairo_line_to(ctx->cr, x, y - to[cur_index]);
710 x += 1.0;
712 /* cycles around the index */
713 if(grow == Right)
715 if (++cur_index > w - 1)
716 cur_index = 0;
718 else
720 if(--cur_index < 0)
721 cur_index = w - 1;
725 /* onto the right border: fills a pixel also when there's only one value */
726 if(grow == Right)
727 cairo_line_to(ctx->cr, x, y - to[cur_index]);
729 cairo_stroke(ctx->cr);
731 if(pat)
732 cairo_pattern_destroy(pat);
733 /* reset line-width */
734 cairo_set_line_width(ctx->cr, 1.0);
737 /** Draw an image from ARGB data to a draw context.
738 * Data should be stored as an array of alpha, red, blue, green for each pixel
739 * and the array size should be w * h elements long.
740 * \param ctx Draw context to draw to.
741 * \param x X coordinate.
742 * \param y Y coordinate.
743 * \param w Width.
744 * \param h Height.
745 * \param wanted_h Wanted height: if > 0, image will be resized.
746 * \param data The image pixels array.
748 void
749 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
750 int wanted_h, unsigned char *data)
752 double ratio;
753 cairo_t *cr;
754 cairo_surface_t *source;
756 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
757 #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)
758 sizeof(unsigned char) * 4 * w);
759 #else
760 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
761 #endif
762 cr = cairo_create(ctx->surface);
763 if(wanted_h > 0 && h > 0)
765 ratio = (double) wanted_h / (double) h;
766 cairo_scale(cr, ratio, ratio);
767 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
769 else
770 cairo_set_source_surface(cr, source, x, y);
772 cairo_paint(cr);
774 cairo_destroy(cr);
775 cairo_surface_destroy(source);
778 #ifndef WITH_IMLIB2
780 /** Load an image from filename.
781 * \param filename The image file to load.
782 * \return A new image.
784 draw_image_t *
785 draw_image_new(const char *filename)
787 draw_image_t *image = NULL;
788 GdkPixbuf *pixbuf;
789 GError *error = NULL;
791 if(filename)
793 if(!(pixbuf = gdk_pixbuf_new_from_file(filename,&error)))
794 warn("cannot load image %s: %s", filename, error->message);
795 else
797 image = p_new(draw_image_t, 1);
798 image->data = pixbuf;
799 image->width = gdk_pixbuf_get_width(pixbuf);
800 image->height = gdk_pixbuf_get_height(pixbuf);
804 return image;
807 /** Delete an image.
808 * \param image The image to delete.
810 void
811 draw_image_delete(draw_image_t **image)
813 if(*image)
815 gdk_pixbuf_unref((*image)->data);
816 p_delete(image);
820 /** Draw an image to a draw context.
821 * \param ctx Draw context to draw to.
822 * \param x x coordinate.
823 * \param y y coordinate.
824 * \param wanted_h Wanted height: if > 0, image will be resized.
825 * \param image The image to draw.
827 void
828 draw_image(draw_context_t *ctx, int x, int y, int wanted_h, draw_image_t *image)
830 cairo_t *cr;
832 cr = cairo_create(ctx->surface);
833 if(wanted_h > 0 && image->height > 0)
835 double ratio = (double) wanted_h / (double) image->height;
836 cairo_scale(cr, ratio, ratio);
837 gdk_cairo_set_source_pixbuf(cr, image->data, x / ratio, y / ratio);
839 else
840 gdk_cairo_set_source_pixbuf(cr, image->data, x, y);
842 cairo_paint(cr);
844 cairo_destroy(cr);
847 #else /* WITH_IMLIB2 */
849 static const char *
850 draw_imlib_load_strerror(Imlib_Load_Error e)
852 switch(e)
854 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
855 return "no such file or directory";
856 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
857 return "file is a directory";
858 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
859 return "read permission denied";
860 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
861 return "no loader for file format";
862 case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
863 return "path too long";
864 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
865 return "path component non existant";
866 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
867 return "path compoment not a directory";
868 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
869 return "path points oustide address space";
870 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
871 return "too many symbolic links";
872 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
873 return "out of memory";
874 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
875 return "out of file descriptors";
876 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
877 return "write permission denied";
878 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
879 return "out of disk space";
880 case IMLIB_LOAD_ERROR_UNKNOWN:
881 return "unknown error, that's really bad";
882 case IMLIB_LOAD_ERROR_NONE:
883 return "no error, oops";
886 return "unknown error";
890 /** Load an image from filename.
891 * \param filename The image file to load.
892 * \return A new image.
894 draw_image_t *
895 draw_image_new(const char *filename)
897 int w, h, size, i;
898 DATA32 *data;
899 double alpha;
900 unsigned char *dataimg, *rdataimg;
901 Imlib_Image imimage;
902 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
903 draw_image_t *image;
905 if(!filename)
906 return NULL;
908 if(!(imimage = imlib_load_image_with_error_return(filename, &e)))
910 warn("cannot load image %s: %s", filename, draw_imlib_load_strerror(e));
911 return NULL;
914 imlib_context_set_image(imimage);
916 w = imlib_image_get_width();
917 h = imlib_image_get_height();
919 size = w * h;
921 data = imlib_image_get_data_for_reading_only();
923 rdataimg = dataimg = p_new(unsigned char, size * 4);
925 for(i = 0; i < size; i++, dataimg += 4)
927 dataimg[3] = (data[i] >> 24) & 0xff; /* A */
928 /* cairo wants pre-multiplied alpha */
929 alpha = dataimg[3] / 255.0;
930 dataimg[2] = ((data[i] >> 16) & 0xff) * alpha; /* R */
931 dataimg[1] = ((data[i] >> 8) & 0xff) * alpha; /* G */
932 dataimg[0] = (data[i] & 0xff) * alpha; /* B */
935 image = p_new(draw_image_t, 1);
937 image->data = rdataimg;
938 image->width = w;
939 image->height = h;
941 imlib_free_image();
943 return image;
946 /** Delete an image.
947 * \param image The image to delete.
949 void
950 draw_image_delete(draw_image_t **image)
952 if (*image)
954 p_delete(&(*image)->data);
955 p_delete(image);
959 /** Draw an image to a draw context.
960 * \param ctx Draw context to draw to.
961 * \param x X coordinate.
962 * \param y Y coordinate.
963 * \param wanted_h Wanted height: if > 0, image will be resized.
964 * \param image The image to draw.
966 void
967 draw_image(draw_context_t *ctx, int x, int y, int wanted_h, draw_image_t *image)
969 draw_image_from_argb_data(ctx, x, y, image->width, image->height, wanted_h, image->data);
972 #endif /* WITH_IMLIB2 */
974 /** Rotate a pixmap.
975 * \param ctx Draw context to draw with.
976 * \param src Drawable to draw from.
977 * \param dest Drawable to draw to.
978 * \param src_w Drawable width.
979 * \param src_h Drawable height.
980 * \param dest_w Drawable width.
981 * \param dest_h Drawable height.
982 * \param angle angle to rotate.
983 * \param tx Translate to this x coordinate.
984 * \param ty Translate to this y coordinate.
986 void
987 draw_rotate(draw_context_t *ctx,
988 xcb_pixmap_t src, xcb_pixmap_t dest,
989 int src_w, int src_h,
990 int dest_w, int dest_h,
991 double angle, int tx, int ty)
993 cairo_surface_t *surface, *source;
994 cairo_t *cr;
996 surface = cairo_xcb_surface_create(ctx->connection, dest,
997 ctx->visual, dest_w, dest_h);
998 source = cairo_xcb_surface_create(ctx->connection, src,
999 ctx->visual, src_w, src_h);
1000 cr = cairo_create (surface);
1002 cairo_translate(cr, tx, ty);
1003 cairo_rotate(cr, angle);
1005 cairo_set_source_surface(cr, source, 0.0, 0.0);
1006 cairo_paint(cr);
1008 cairo_destroy(cr);
1009 cairo_surface_destroy(source);
1010 cairo_surface_destroy(surface);
1013 /** Return the width and height of a text in pixel.
1014 * \param conn Connection ref.
1015 * \param phys_screen Physical screen number.
1016 * \param font Font to use.
1017 * \param text The text.
1018 * \param len The text length.
1019 * \param pdata The parser data to fill.
1020 * \return Text height and width.
1022 area_t
1023 draw_text_extents(xcb_connection_t *conn, int phys_screen, font_t *font,
1024 const char *text, ssize_t len, draw_parser_data_t *parser_data)
1026 cairo_surface_t *surface;
1027 cairo_t *cr;
1028 PangoLayout *layout;
1029 PangoRectangle ext;
1030 xcb_screen_t *s = xutil_screen_get(conn, phys_screen);
1031 area_t geom = { 0, 0, 0, 0 };
1033 if(!len)
1034 return geom;
1036 parser_data->connection = conn;
1037 parser_data->phys_screen = phys_screen;
1038 if(draw_text_markup_expand(parser_data, text, len))
1040 text = parser_data->text;
1041 len = parser_data->len;
1044 surface = cairo_xcb_surface_create(conn, phys_screen,
1045 draw_screen_default_visual(s),
1046 s->width_in_pixels,
1047 s->height_in_pixels);
1049 cr = cairo_create(surface);
1050 layout = pango_cairo_create_layout(cr);
1051 pango_layout_set_text(layout, parser_data->text, parser_data->len);
1052 pango_layout_set_attributes(layout, parser_data->attr_list);
1053 pango_layout_set_font_description(layout, font->desc);
1054 pango_layout_get_pixel_extents(layout, NULL, &ext);
1055 g_object_unref(layout);
1056 cairo_destroy(cr);
1057 cairo_surface_destroy(surface);
1059 geom.width = ext.width;
1060 geom.height = ext.height * 1.5;
1062 return geom;
1065 /** Transform a string to a alignment_t type.
1066 * Recognized string are left, center or right. Everything else will be
1067 * recognized as AlignAuto.
1068 * \param align Atring with align text.
1069 * \param len The string length.
1070 * \return An alignment_t type.
1072 alignment_t
1073 draw_align_fromstr(const char *align, ssize_t len)
1075 switch (a_tokenize(align, len))
1077 case A_TK_LEFT: return AlignLeft;
1078 case A_TK_CENTER: return AlignCenter;
1079 case A_TK_RIGHT: return AlignRight;
1080 case A_TK_FLEX: return AlignFlex;
1081 default: return AlignAuto;
1085 /** Transform an alignment to a string.
1086 * \param a The alignment.
1087 * \return A string which must not be freed.
1089 const char *
1090 draw_align_tostr(alignment_t a)
1092 switch(a)
1094 case AlignLeft: return "left";
1095 case AlignCenter: return "center";
1096 case AlignRight: return "right";
1097 case AlignFlex: return "flex";
1098 case AlignAuto: return "auto";
1099 default: return NULL;
1103 #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
1105 /** Send a request to initialize a X color.
1106 * \param conn Connection ref.
1107 * \param color xcolor_t struct to store color into.
1108 * \param phys_screen Physical screen number.
1109 * \param colstr Color specification.
1110 * \return request informations.
1112 xcolor_init_request_t
1113 xcolor_init_unchecked(xcb_connection_t *conn, xcolor_t *color, int phys_screen,
1114 const char *colstr, ssize_t len)
1116 xcb_screen_t *s = xutil_screen_get(conn, phys_screen);
1117 xcolor_init_request_t req;
1118 unsigned long colnum;
1119 uint16_t red, green, blue;
1121 p_clear(&req, 1);
1123 if(!len)
1125 req.has_error = true;
1126 return req;
1129 req.alpha = 0xffff;
1130 req.color = color;
1132 /* The color is given in RGB value */
1133 if(colstr[0] == '#')
1135 char *p;
1137 if(len == 7)
1139 colnum = strtoul(colstr + 1, &p, 16);
1140 if(p - colstr != 7)
1141 goto invalid;
1143 /* we have alpha */
1144 else if(len == 9)
1146 colnum = strtoul(colstr + 1, &p, 16);
1147 if(p - colstr != 9)
1148 goto invalid;
1149 req.alpha = RGB_COLOR_8_TO_16(colnum);
1150 colnum >>= 8;
1152 else
1154 invalid:
1155 warn("awesome: error, invalid color '%s'", colstr);
1156 req.has_error = true;
1157 return req;
1160 red = RGB_COLOR_8_TO_16(colnum >> 16);
1161 green = RGB_COLOR_8_TO_16(colnum >> 8);
1162 blue = RGB_COLOR_8_TO_16(colnum);
1164 req.is_hexa = true;
1165 req.cookie_hexa = xcb_alloc_color_unchecked(conn, s->default_colormap,
1166 red, green, blue);
1168 else
1170 req.is_hexa = false;
1171 req.cookie_named = xcb_alloc_named_color_unchecked(conn, s->default_colormap, len,
1172 colstr);
1175 req.has_error = false;
1176 req.colstr = colstr;
1178 return req;
1181 /** Initialize a X color.
1182 * \param conn Connection ref.
1183 * \param req xcolor_init request.
1184 * \return True if color allocation was successfull.
1186 bool
1187 xcolor_init_reply(xcb_connection_t *conn,
1188 xcolor_init_request_t req)
1190 if(req.has_error)
1191 return false;
1193 if(req.is_hexa)
1195 xcb_alloc_color_reply_t *hexa_color;
1197 if((hexa_color = xcb_alloc_color_reply(conn, req.cookie_hexa, NULL)))
1199 req.color->pixel = hexa_color->pixel;
1200 req.color->red = hexa_color->red;
1201 req.color->green = hexa_color->green;
1202 req.color->blue = hexa_color->blue;
1203 req.color->alpha = req.alpha;
1204 req.color->initialized = true;
1205 p_delete(&hexa_color);
1206 return true;
1209 else
1211 xcb_alloc_named_color_reply_t *named_color;
1213 if((named_color = xcb_alloc_named_color_reply(conn, req.cookie_named, NULL)))
1215 req.color->pixel = named_color->pixel;
1216 req.color->red = named_color->visual_red;
1217 req.color->green = named_color->visual_green;
1218 req.color->blue = named_color->visual_blue;
1219 req.color->alpha = req.alpha;
1220 req.color->initialized = true;
1221 p_delete(&named_color);
1222 return true;
1226 warn("awesome: error, cannot allocate color '%s'", req.colstr);
1227 return false;
1230 /** Remove a area from a list of them,
1231 * spliting the space between several area that can overlap
1232 * \param areas Array of areas.
1233 * \param elem Area to remove.
1235 void
1236 area_array_remove(area_array_t *areas, area_t elem)
1238 /* loop from the end because:
1239 * (1) we remove elements ;
1240 * (2) the one we add to the end are okay wrt the invariants
1242 for(int i = areas->len - 1; i >= 0; i--)
1243 if(area_intersect_area(areas->tab[i], elem))
1245 /* remove it from the list */
1246 area_t r = area_array_take(areas, i);
1247 area_t inter = area_get_intersect_area(r, elem);
1249 if(AREA_LEFT(inter) > AREA_LEFT(r))
1251 area_t extra =
1253 .x = r.x,
1254 .y = r.y,
1255 .width = AREA_LEFT(inter) - AREA_LEFT(r),
1256 .height = r.height,
1258 area_array_append(areas, extra);
1261 if(AREA_TOP(inter) > AREA_TOP(r))
1263 area_t extra =
1265 .x = r.x,
1266 .y = r.y,
1267 .width = r.width,
1268 .height = AREA_TOP(inter) - AREA_LEFT(r),
1270 area_array_append(areas, extra);
1273 if(AREA_RIGHT(inter) < AREA_RIGHT(r))
1275 area_t extra =
1277 .x = AREA_RIGHT(inter),
1278 .y = r.y,
1279 .width = AREA_RIGHT(r) - AREA_RIGHT(inter),
1280 .height = r.height,
1282 area_array_append(areas, extra);
1285 if(AREA_BOTTOM(inter) < AREA_BOTTOM(r))
1287 area_t extra =
1289 .x = r.x,
1290 .y = AREA_BOTTOM(inter),
1291 .width = r.width,
1292 .height = AREA_BOTTOM(r) - AREA_BOTTOM(inter),
1294 area_array_append(areas, extra);
1299 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80