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>
34 #include "common/tokenize.h"
35 #include "common/markup.h"
37 extern awesome_t globalconf
;
39 /** Convert text from any charset to UTF-8 using iconv.
40 * \param iso The ISO string to convert.
41 * \param len The string size.
42 * \return NULL if error, otherwise pointer to the new converted string.
45 draw_iso2utf8(const char *iso
, size_t len
)
49 static iconv_t iso2utf8
= (iconv_t
) -1;
54 if(!a_strcmp(nl_langinfo(CODESET
), "UTF-8"))
57 if(iso2utf8
== (iconv_t
) -1)
59 iso2utf8
= iconv_open("UTF-8", nl_langinfo(CODESET
));
60 if(iso2utf8
== (iconv_t
) -1)
63 warn("unable to convert text from %s to UTF-8, not available",
64 nl_langinfo(CODESET
));
66 warn("unable to convert text: %s", strerror(errno
));
72 utf8len
= 2 * len
+ 1;
73 utf8
= utf8p
= p_new(char, utf8len
);
75 if(iconv(iso2utf8
, (char **) &iso
, &len
, &utf8
, &utf8len
) == (size_t) -1)
77 warn("text conversion failed: %s", strerror(errno
));
84 static xcb_visualtype_t
*
85 draw_screen_default_visual(xcb_screen_t
*s
)
87 xcb_depth_iterator_t depth_iter
;
88 xcb_visualtype_iterator_t visual_iter
;
93 for(depth_iter
= xcb_screen_allowed_depths_iterator(s
);
94 depth_iter
.rem
; xcb_depth_next (&depth_iter
))
95 for(visual_iter
= xcb_depth_visuals_iterator (depth_iter
.data
);
96 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
97 if (s
->root_visual
== visual_iter
.data
->visual_id
)
98 return visual_iter
.data
;
103 /** Create a new Pango font.
104 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
105 * \return A new font.
108 draw_font_new(const char *fontname
)
110 cairo_surface_t
*surface
;
111 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
114 font_t
*font
= p_new(font_t
, 1);
116 /* Create a dummy cairo surface, cairo context and pango layout in
117 * order to get font informations */
118 surface
= cairo_xcb_surface_create(globalconf
.connection
,
119 globalconf
.default_screen
,
120 draw_screen_default_visual(s
),
122 s
->height_in_pixels
);
124 cr
= cairo_create(surface
);
125 layout
= pango_cairo_create_layout(cr
);
127 /* Get the font description used to set text on a PangoLayout */
128 font
->desc
= pango_font_description_from_string(fontname
);
129 pango_layout_set_font_description(layout
, font
->desc
);
132 pango_layout_get_pixel_size(layout
, NULL
, &font
->height
);
134 g_object_unref(layout
);
136 cairo_surface_destroy(surface
);
142 * \param font Font to delete.
145 draw_font_delete(font_t
**font
)
149 pango_font_description_free((*font
)->desc
);
155 draw_markup_on_element(markup_parser_data_t
*p
, const char *elem
,
156 const char **names
, const char **values
)
158 draw_parser_data_t
*data
= p
->priv
;
160 xcolor_init_request_t reqs
[3];
161 int8_t i
, bg_color_nbr
= -1, reqs_nbr
= -1;
163 /* hack: markup.c validates tags so we can avoid strcmps here */
166 if(elem
[1] == 'g') /* bg? */
168 if(elem
[2] == '_') /* bg_margin */
169 for(; *names
; names
++, values
++)
170 switch(a_tokenize(*names
, -1))
173 data
->bg_margin
.left
= atoi(*values
);
176 data
->bg_margin
.top
= atoi(*values
);
181 for(; *names
; names
++, values
++)
182 switch(a_tokenize(*names
, -1))
185 reqs
[++reqs_nbr
] = xcolor_init_unchecked(&data
->bg_color
,
189 bg_color_nbr
= reqs_nbr
;
193 image_delete(&data
->bg_image
);
194 data
->bg_image
= image_new_from_file(*values
);
197 data
->bg_align
= draw_align_fromstr(*values
, -1);
200 data
->bg_resize
= a_strtobool(*values
, -1);
207 for(; *names
; names
++, values
++)
208 switch(a_tokenize(*names
, -1))
211 reqs
[++reqs_nbr
] = xcolor_init_unchecked(&data
->border
.color
,
216 data
->border
.width
= atoi(*values
);
223 for(; *names
; names
++, values
++)
224 switch(a_tokenize(*names
, -1))
227 data
->align
= draw_align_fromstr(*values
, -1);
230 reqs
[++reqs_nbr
] = xcolor_init_unchecked(&data
->shadow
.color
,
234 case A_TK_SHADOW_OFFSET
:
235 data
->shadow
.offset
= atoi(*values
);
241 case 'm': /* margin */
242 for (; *names
; names
++, values
++)
243 switch(a_tokenize(*names
, -1))
246 data
->margin
.left
= atoi(*values
);
249 data
->margin
.right
= atoi(*values
);
252 data
->margin
.top
= atoi(*values
);
259 for(i
= 0; i
<= reqs_nbr
; i
++)
260 if(i
== bg_color_nbr
)
261 data
->has_bg_color
= xcolor_init_reply(reqs
[i
]);
263 xcolor_init_reply(reqs
[i
]);
267 draw_text_markup_expand(draw_parser_data_t
*data
,
268 const char *str
, ssize_t slen
)
270 static char const * const elements
[] = { "bg", "bg_margin", "text", "margin", "border", NULL
};
271 markup_parser_data_t p
=
273 .elements
= elements
,
275 .on_element
= &draw_markup_on_element
,
277 GError
*error
= NULL
;
280 markup_parser_data_init(&p
);
282 if(!markup_parse(&p
, str
, slen
))
285 if(!pango_parse_markup(p
.text
.s
, p
.text
.len
, 0, &data
->attr_list
, &data
->text
, NULL
, &error
))
287 warn("cannot parse pango markup: %s", error
? error
->message
: "unknown error");
293 data
->len
= a_strlen(data
->text
);
297 markup_parser_data_wipe(&p
);
301 /** Initialize a new draw context.
302 * \param d The draw context to initialize.
303 * \param phys_screen Physical screen id.
304 * \param width Width.
305 * \param height Height.
306 * \param px Pixmap object to store.
307 * \param fg Foreground color.
308 * \param bg Background color.
311 draw_context_init(draw_context_t
*d
, int phys_screen
,
312 int width
, int height
, xcb_pixmap_t px
,
313 const xcolor_t
*fg
, const xcolor_t
*bg
)
315 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
317 d
->phys_screen
= phys_screen
;
320 d
->depth
= s
->root_depth
;
321 d
->visual
= draw_screen_default_visual(s
);
323 d
->surface
= cairo_xcb_surface_create(globalconf
.connection
, px
, d
->visual
, width
, height
);
324 d
->cr
= cairo_create(d
->surface
);
325 d
->layout
= pango_cairo_create_layout(d
->cr
);
330 /** Draw text into a draw context.
331 * \param ctx Draw context to draw to.
332 * \param font The font to use.
333 * \param area Area to draw to.
334 * \param text Text to draw.
335 * \param len Text to draw length.
336 * \param data Optional parser data.
339 draw_text(draw_context_t
*ctx
, font_t
*font
,
340 area_t area
, const char *text
, ssize_t len
, draw_parser_data_t
*pdata
)
344 draw_parser_data_t parser_data
;
348 draw_parser_data_init(&parser_data
);
349 if(draw_text_markup_expand(&parser_data
, text
, len
))
351 text
= parser_data
.text
;
352 len
= parser_data
.len
;
354 pdata
= &parser_data
;
362 if(pdata
->has_bg_color
)
363 draw_rectangle(ctx
, area
, 1.0, true, &pdata
->bg_color
);
365 if(pdata
->border
.width
> 0)
366 draw_rectangle(ctx
, area
, pdata
->border
.width
, false, &pdata
->border
.color
);
372 switch(pdata
->bg_align
)
375 x
+= (area
.width
- pdata
->bg_image
->width
) / 2;
378 x
+= area
.width
- pdata
->bg_image
->width
;
384 x
+ pdata
->bg_margin
.left
,
385 y
+ pdata
->bg_margin
.top
,
386 pdata
->bg_resize
? area
.height
: 0, pdata
->bg_image
);
389 pango_layout_set_text(ctx
->layout
, pdata
->text
, pdata
->len
);
390 pango_layout_set_width(ctx
->layout
,
391 pango_units_from_double(area
.width
392 - (pdata
->margin
.left
393 + pdata
->margin
.right
)));
394 pango_layout_set_height(ctx
->layout
, pango_units_from_double(area
.height
));
395 pango_layout_set_ellipsize(ctx
->layout
, PANGO_ELLIPSIZE_END
);
396 pango_layout_set_wrap(ctx
->layout
, PANGO_WRAP_WORD_CHAR
);
397 pango_layout_set_attributes(ctx
->layout
, pdata
->attr_list
);
398 pango_layout_set_font_description(ctx
->layout
, font
->desc
);
399 pango_layout_get_pixel_extents(ctx
->layout
, NULL
, &ext
);
401 x
= area
.x
+ pdata
->margin
.left
;
402 /* + 1 is added for rounding, so that in any case of doubt we rather draw
403 * the text 1px lower than too high which usually results in a better type
405 y
= area
.y
+ (ctx
->height
- ext
.height
+ 1) / 2 + pdata
->margin
.top
;
410 x
+= (area
.width
- ext
.width
) / 2;
413 x
+= area
.width
- ext
.width
;
419 if(pdata
->shadow
.offset
)
421 cairo_set_source_rgba(ctx
->cr
,
422 pdata
->shadow
.color
.red
/ 65535.0,
423 pdata
->shadow
.color
.green
/ 65535.0,
424 pdata
->shadow
.color
.blue
/ 65535.0,
425 pdata
->shadow
.color
.alpha
/ 65535.0);
426 cairo_move_to(ctx
->cr
, x
+ pdata
->shadow
.offset
, y
+ pdata
->shadow
.offset
);
427 pango_cairo_layout_path(ctx
->cr
, ctx
->layout
);
428 cairo_stroke(ctx
->cr
);
431 cairo_move_to(ctx
->cr
, x
, y
);
433 cairo_set_source_rgba(ctx
->cr
,
434 ctx
->fg
.red
/ 65535.0,
435 ctx
->fg
.green
/ 65535.0,
436 ctx
->fg
.blue
/ 65535.0,
437 ctx
->fg
.alpha
/ 65535.0);
438 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
439 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
441 if (pdata
== &parser_data
)
442 draw_parser_data_wipe(&parser_data
);
445 /** Setup color-source for cairo (gradient or mono).
446 * \param ctx Draw context.
447 * \param gradient_vector x, y to x + x_offset, y + y_offset.
448 * \param pcolor Color to use at start of gradient_vector.
449 * \param pcolor_center Color at center of gradient_vector.
450 * \param pcolor_end Color at end of gradient_vector.
451 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
453 static cairo_pattern_t
*
454 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
455 const xcolor_t
*pcolor
, const xcolor_t
*pcolor_center
,
456 const xcolor_t
*pcolor_end
)
458 cairo_pattern_t
*pat
= NULL
;
459 bool has_center
= pcolor_center
->initialized
;
460 bool has_end
= pcolor_end
->initialized
;
462 /* no need for a real pattern: */
463 if(!has_end
&& !has_center
)
464 cairo_set_source_rgba(ctx
->cr
,
465 pcolor
->red
/ 65535.0,
466 pcolor
->green
/ 65535.0,
467 pcolor
->blue
/ 65535.0,
468 pcolor
->alpha
/ 65535.0);
471 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
473 gradient_vector
.x
+ gradient_vector
.x_offset
,
474 gradient_vector
.y
+ gradient_vector
.y_offset
);
476 /* pcolor is always set (so far in awesome) */
477 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
478 pcolor
->red
/ 65535.0,
479 pcolor
->green
/ 65535.0,
480 pcolor
->blue
/ 65535.0,
481 pcolor
->alpha
/ 65535.0);
484 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
485 pcolor_center
->red
/ 65535.0,
486 pcolor_center
->green
/ 65535.0,
487 pcolor_center
->blue
/ 65535.0,
488 pcolor_center
->alpha
/ 65535.0);
491 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
492 pcolor_end
->red
/ 65535.0,
493 pcolor_end
->green
/ 65535.0,
494 pcolor_end
->blue
/ 65535.0,
495 pcolor_end
->alpha
/ 65535.0);
497 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
498 pcolor
->red
/ 65535.0,
499 pcolor
->green
/ 65535.0,
500 pcolor
->blue
/ 65535.0,
501 pcolor
->alpha
/ 65535.0);
502 cairo_set_source(ctx
->cr
, pat
);
507 /** Draw rectangle inside the coordinates
508 * \param ctx Draw context
509 * \param geometry geometry
510 * \param line_width line width
511 * \param filled fill rectangle?
512 * \param color color to use
515 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
516 float line_width
, bool filled
, const xcolor_t
*color
)
518 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
519 cairo_set_line_width(ctx
->cr
, line_width
);
520 cairo_set_miter_limit(ctx
->cr
, 10.0);
521 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
522 cairo_set_source_rgba(ctx
->cr
,
523 color
->red
/ 65535.0,
524 color
->green
/ 65535.0,
525 color
->blue
/ 65535.0,
526 color
->alpha
/ 65535.0);
529 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
530 geometry
.width
, geometry
.height
);
535 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
536 geometry
.width
- line_width
, geometry
.height
- line_width
);
537 cairo_stroke(ctx
->cr
);
541 /** Draw rectangle with gradient colors
542 * \param ctx Draw context.
543 * \param geometry Geometry.
544 * \param line_width Line width.
545 * \param filled Filled rectangle?
546 * \param gradient_vector Color-gradient course.
547 * \param pcolor Color at start of gradient_vector.
548 * \param pcolor_center Color in the center.
549 * \param pcolor_end Color at end of gradient_vector.
552 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
553 vector_t gradient_vector
, const xcolor_t
*pcolor
,
554 const xcolor_t
*pcolor_center
, const xcolor_t
*pcolor_end
)
556 cairo_pattern_t
*pat
;
558 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
559 cairo_set_line_width(ctx
->cr
, line_width
);
560 cairo_set_miter_limit(ctx
->cr
, 10.0);
561 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
563 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
567 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
572 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
573 cairo_stroke(ctx
->cr
);
577 cairo_pattern_destroy(pat
);
580 /** Setup some cairo-things for drawing a graph
581 * \param ctx Draw context
584 draw_graph_setup(draw_context_t
*ctx
)
586 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
587 cairo_set_line_width(ctx
->cr
, 1.0);
588 /* without it, it can draw over the path on sharp angles
589 * ...too long lines * (...graph_line) */
590 cairo_set_miter_limit(ctx
->cr
, 0.0);
591 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
595 * \param ctx Draw context.
596 * \param rect The area to draw into.
597 * \param from Array of starting-point offsets to draw a graph lines.
598 * \param to Array of end-point offsets to draw a graph lines.
599 * \param cur_index Current position in data-array (cycles around).
600 * \param grow Put new values to the left or to the right.
601 * \param gradient_vector Color-Gradient course.
602 * \param pcolor Color at start of gradient_vector.
603 * \param pcolor_center Color in the center.
604 * \param pcolor_end Color at end of gradient_vector.
607 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
608 position_t grow
, vector_t gradient_vector
, const xcolor_t
*pcolor
,
609 const xcolor_t
*pcolor_center
, const xcolor_t
*pcolor_end
)
612 float x
= rect
.x
+ 0.5; /* middle of a pixel */
613 cairo_pattern_t
*pat
;
615 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
616 pcolor
, pcolor_center
, pcolor_end
);
618 if(grow
== Right
) /* draw from right to left */
621 while(++i
< rect
.width
)
623 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
624 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
628 cur_index
= rect
.width
- 1;
631 else /* draw from left to right */
632 while(++i
< rect
.width
)
634 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
635 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
639 cur_index
= rect
.width
- 1;
642 cairo_stroke(ctx
->cr
);
645 cairo_pattern_destroy(pat
);
648 /** Draw a line into a graph-widget.
649 * \param ctx Draw context.
650 * \param rect The area to draw into.
651 * \param to array of offsets to draw the line through...
652 * \param cur_index current position in data-array (cycles around)
653 * \param grow put new values to the left or to the right
654 * \param gradient_vector Color-gradient course.
655 * \param pcolor Color at start of gradient_vector.
656 * \param pcolor_center Color in the center.
657 * \param pcolor_end Color at end of gradient_vector.
660 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
661 position_t grow
, vector_t gradient_vector
, const xcolor_t
*pcolor
,
662 const xcolor_t
*pcolor_center
, const xcolor_t
*pcolor_end
)
666 cairo_pattern_t
*pat
;
668 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
669 * ... it won't fill some pixels! It also looks much nicer so.
670 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
671 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
672 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
673 /* a nicer, better visible line compared to 1.0 */
674 cairo_set_line_width(ctx
->cr
, 1.25);
676 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
678 /* path through the centers of pixels */
685 /* go through the values from old to new. Begin with the oldest. */
686 if(++cur_index
> w
- 1)
689 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
692 /* on the left border: fills a pixel also when there's only one value */
693 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
695 for(i
= 0; i
< w
; i
++)
697 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
700 /* cycles around the index */
703 if (++cur_index
> w
- 1)
713 /* onto the right border: fills a pixel also when there's only one value */
715 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
717 cairo_stroke(ctx
->cr
);
720 cairo_pattern_destroy(pat
);
721 /* reset line-width */
722 cairo_set_line_width(ctx
->cr
, 1.0);
725 /** Draw an image from ARGB data to a draw context.
726 * Data should be stored as an array of alpha, red, blue, green for each pixel
727 * and the array size should be w * h elements long.
728 * \param ctx Draw context to draw to.
729 * \param x X coordinate.
730 * \param y Y coordinate.
733 * \param wanted_h Wanted height: if > 0, image will be resized.
734 * \param data The image pixels array.
737 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
738 int wanted_h
, unsigned char *data
)
742 cairo_surface_t
*source
;
744 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
745 #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)
746 sizeof(unsigned char) * 4 * w
);
748 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
750 cr
= cairo_create(ctx
->surface
);
751 if(wanted_h
> 0 && h
> 0)
753 ratio
= (double) wanted_h
/ (double) h
;
754 cairo_scale(cr
, ratio
, ratio
);
755 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
758 cairo_set_source_surface(cr
, source
, x
, y
);
763 cairo_surface_destroy(source
);
766 /** Draw an image to a draw context.
767 * \param ctx Draw context to draw to.
768 * \param x X coordinate.
769 * \param y Y coordinate.
770 * \param wanted_h Wanted height: if > 0, image will be resized.
771 * \param image The image to draw.
774 draw_image(draw_context_t
*ctx
, int x
, int y
, int wanted_h
, image_t
*image
)
776 draw_image_from_argb_data(ctx
, x
, y
, image
->width
, image
->height
, wanted_h
, image
->data
);
780 * \param ctx Draw context to draw with.
781 * \param src Drawable to draw from.
782 * \param dest Drawable to draw to.
783 * \param src_w Drawable width.
784 * \param src_h Drawable height.
785 * \param dest_w Drawable width.
786 * \param dest_h Drawable height.
787 * \param angle angle to rotate.
788 * \param tx Translate to this x coordinate.
789 * \param ty Translate to this y coordinate.
792 draw_rotate(draw_context_t
*ctx
,
793 xcb_pixmap_t src
, xcb_pixmap_t dest
,
794 int src_w
, int src_h
,
795 int dest_w
, int dest_h
,
796 double angle
, int tx
, int ty
)
798 cairo_surface_t
*surface
, *source
;
801 surface
= cairo_xcb_surface_create(globalconf
.connection
, dest
,
802 ctx
->visual
, dest_w
, dest_h
);
803 source
= cairo_xcb_surface_create(globalconf
.connection
, src
,
804 ctx
->visual
, src_w
, src_h
);
805 cr
= cairo_create (surface
);
807 cairo_translate(cr
, tx
, ty
);
808 cairo_rotate(cr
, angle
);
810 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
814 cairo_surface_destroy(source
);
815 cairo_surface_destroy(surface
);
818 /** Return the width and height of a text in pixel.
819 * \param font Font to use.
820 * \param text The text.
821 * \param len The text length.
822 * \param pdata The parser data to fill.
823 * \return Text height and width.
826 draw_text_extents(font_t
*font
, const char *text
, ssize_t len
, draw_parser_data_t
*parser_data
)
828 cairo_surface_t
*surface
;
832 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
833 area_t geom
= { 0, 0, 0, 0 };
838 if(!draw_text_markup_expand(parser_data
, text
, len
))
841 surface
= cairo_xcb_surface_create(globalconf
.connection
,
842 globalconf
.default_screen
,
843 draw_screen_default_visual(s
),
845 s
->height_in_pixels
);
847 cr
= cairo_create(surface
);
848 layout
= pango_cairo_create_layout(cr
);
849 pango_layout_set_text(layout
, parser_data
->text
, parser_data
->len
);
850 pango_layout_set_attributes(layout
, parser_data
->attr_list
);
851 pango_layout_set_font_description(layout
, font
->desc
);
852 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
853 g_object_unref(layout
);
855 cairo_surface_destroy(surface
);
857 geom
.width
= ext
.width
;
858 geom
.height
= ext
.height
* 1.5;
863 /** Transform a string to a alignment_t type.
864 * Recognized string are flex, left, center or right. Everything else will be
865 * recognized as AlignLeft.
866 * \param align Atring with align text.
867 * \param len The string length.
868 * \return An alignment_t type.
871 draw_align_fromstr(const char *align
, ssize_t len
)
873 switch (a_tokenize(align
, len
))
875 case A_TK_CENTER
: return AlignCenter
;
876 case A_TK_RIGHT
: return AlignRight
;
877 case A_TK_FLEX
: return AlignFlex
;
878 case A_TK_TOP
: return AlignTop
;
879 case A_TK_BOTTOM
: return AlignBottom
;
880 default: return AlignLeft
;
884 /** Transform an alignment to a string.
885 * \param a The alignment.
886 * \return A string which must not be freed.
889 draw_align_tostr(alignment_t a
)
893 case AlignLeft
: return "left";
894 case AlignCenter
: return "center";
895 case AlignRight
: return "right";
896 case AlignFlex
: return "flex";
897 case AlignBottom
: return "bottom";
898 case AlignTop
: return "top";
899 default: return NULL
;
903 #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
905 /** Send a request to initialize a X color.
906 * \param color xcolor_t struct to store color into.
907 * \param colstr Color specification.
908 * \return request informations.
910 xcolor_init_request_t
911 xcolor_init_unchecked(xcolor_t
*color
, const char *colstr
, ssize_t len
)
913 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
914 xcolor_init_request_t req
;
915 unsigned long colnum
;
916 uint16_t red
, green
, blue
;
922 req
.has_error
= true;
929 /* The color is given in RGB value */
936 colnum
= strtoul(colstr
+ 1, &p
, 16);
943 colnum
= strtoul(colstr
+ 1, &p
, 16);
946 req
.alpha
= RGB_COLOR_8_TO_16(colnum
);
952 warn("awesome: error, invalid color '%s'", colstr
);
953 req
.has_error
= true;
957 red
= RGB_COLOR_8_TO_16(colnum
>> 16);
958 green
= RGB_COLOR_8_TO_16(colnum
>> 8);
959 blue
= RGB_COLOR_8_TO_16(colnum
);
962 req
.cookie_hexa
= xcb_alloc_color_unchecked(globalconf
.connection
,
969 req
.cookie_named
= xcb_alloc_named_color_unchecked(globalconf
.connection
,
970 s
->default_colormap
, len
,
974 req
.has_error
= false;
980 /** Initialize a X color.
981 * \param req xcolor_init request.
982 * \return True if color allocation was successfull.
985 xcolor_init_reply(xcolor_init_request_t req
)
992 xcb_alloc_color_reply_t
*hexa_color
;
994 if((hexa_color
= xcb_alloc_color_reply(globalconf
.connection
,
995 req
.cookie_hexa
, NULL
)))
997 req
.color
->pixel
= hexa_color
->pixel
;
998 req
.color
->red
= hexa_color
->red
;
999 req
.color
->green
= hexa_color
->green
;
1000 req
.color
->blue
= hexa_color
->blue
;
1001 req
.color
->alpha
= req
.alpha
;
1002 req
.color
->initialized
= true;
1003 p_delete(&hexa_color
);
1009 xcb_alloc_named_color_reply_t
*named_color
;
1011 if((named_color
= xcb_alloc_named_color_reply(globalconf
.connection
,
1012 req
.cookie_named
, NULL
)))
1014 req
.color
->pixel
= named_color
->pixel
;
1015 req
.color
->red
= named_color
->visual_red
;
1016 req
.color
->green
= named_color
->visual_green
;
1017 req
.color
->blue
= named_color
->visual_blue
;
1018 req
.color
->alpha
= req
.alpha
;
1019 req
.color
->initialized
= true;
1020 p_delete(&named_color
);
1025 warn("awesome: error, cannot allocate color '%s'", req
.colstr
);
1029 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80