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/xutil.h"
37 /** Convert text from any charset to UTF-8 using iconv.
38 * \param iso The ISO string to convert.
39 * \param len The string size.
40 * \param dest The destination pointer. Memory will be allocated, up to you to
41 * free, like any char *.
42 * \param dlen The destination length, can be NULL.
43 * \return True if conversion was done.
46 draw_iso2utf8(const char *iso
, size_t len
, char **dest
, ssize_t
*dlen
)
48 static iconv_t iso2utf8
= (iconv_t
) -1;
49 static int8_t dont_need_convert
= -1;
51 if(dont_need_convert
== -1)
52 dont_need_convert
= !a_strcmp(nl_langinfo(CODESET
), "UTF-8");
54 if(!len
|| dont_need_convert
)
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 size_t orig_utf8len
, utf8len
;
75 orig_utf8len
= utf8len
= 2 * len
+ 1;
76 utf8
= *dest
= p_new(char, utf8len
);
78 if(iconv(iso2utf8
, (char **) &iso
, &len
, &utf8
, &utf8len
) == (size_t) -1)
80 warn("text conversion failed: %s", strerror(errno
));
86 *dlen
= orig_utf8len
- utf8len
;
91 static xcb_visualtype_t
*
92 draw_screen_default_visual(xcb_screen_t
*s
)
97 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
100 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
101 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator (depth_iter
.data
);
102 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
103 if(s
->root_visual
== visual_iter
.data
->visual_id
)
104 return visual_iter
.data
;
109 /** Create a new Pango font.
110 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
111 * \return A new font.
114 draw_font_new(const char *fontname
)
116 cairo_surface_t
*surface
;
117 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
120 font_t
*font
= p_new(font_t
, 1);
122 xcb_visualtype_t
*visual
= draw_screen_default_visual(s
);
125 fatal("cannot get visual");
127 /* Create a dummy cairo surface, cairo context and pango layout in
128 * order to get font informations */
129 surface
= cairo_xcb_surface_create(globalconf
.connection
,
130 globalconf
.default_screen
,
133 s
->height_in_pixels
);
135 cr
= cairo_create(surface
);
136 layout
= pango_cairo_create_layout(cr
);
138 /* Get the font description used to set text on a PangoLayout */
139 font
->desc
= pango_font_description_from_string(fontname
);
140 pango_layout_set_font_description(layout
, font
->desc
);
143 pango_layout_get_pixel_size(layout
, NULL
, &font
->height
);
145 g_object_unref(layout
);
147 cairo_surface_destroy(surface
);
153 * \param font Font to delete.
156 draw_font_delete(font_t
**font
)
160 pango_font_description_free((*font
)->desc
);
165 /** Initialize a draw_text_context_t with text data.
166 * \param data The draw text context to init.
167 * \param str The text string to render.
168 * \param slen The text string length.
169 * \return True if everything is ok, false otherwise.
172 draw_text_context_init(draw_text_context_t
*data
, const char *str
, ssize_t slen
)
174 GError
*error
= NULL
;
179 if(!pango_parse_markup(str
, slen
, 0, &data
->attr_list
, &data
->text
, NULL
, &error
))
181 warn("cannot parse pango markup: %s", error
? error
->message
: "unknown error");
187 data
->len
= a_strlen(data
->text
);
192 /** Initialize a new draw context.
193 * \param d The draw context to initialize.
194 * \param phys_screen Physical screen id.
195 * \param width Width.
196 * \param height Height.
197 * \param px Pixmap object to store.
198 * \param fg Foreground color.
199 * \param bg Background color.
202 draw_context_init(draw_context_t
*d
, int phys_screen
,
203 int width
, int height
, xcb_pixmap_t px
,
204 const xcolor_t
*fg
, const xcolor_t
*bg
)
206 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
208 d
->phys_screen
= phys_screen
;
211 d
->visual
= draw_screen_default_visual(s
);
213 d
->surface
= cairo_xcb_surface_create(globalconf
.connection
, px
, d
->visual
, width
, height
);
214 d
->cr
= cairo_create(d
->surface
);
215 d
->layout
= pango_cairo_create_layout(d
->cr
);
220 /** Draw text into a draw context.
221 * \param ctx Draw context to draw to.
222 * \param data Draw text context data.
223 * \param elip Ellipsize mode.
224 * \param wrap Wrap mode.
225 * \param align Text alignment.
226 * \param margin Margin to respect when drawing text.
227 * \param area Area to draw to.
228 * \param ext Text extents.
231 draw_text(draw_context_t
*ctx
, draw_text_context_t
*data
,
232 PangoEllipsizeMode ellip
, PangoWrapMode wrap
,
233 alignment_t align
, padding_t
*margin
, area_t area
, area_t
*ext
)
237 pango_layout_set_text(ctx
->layout
, data
->text
, data
->len
);
238 pango_layout_set_width(ctx
->layout
,
239 pango_units_from_double(area
.width
242 pango_layout_set_height(ctx
->layout
, pango_units_from_double(area
.height
)
243 - (margin
->top
+ margin
->bottom
));
244 pango_layout_set_ellipsize(ctx
->layout
, ellip
);
245 pango_layout_set_wrap(ctx
->layout
, wrap
);
246 pango_layout_set_attributes(ctx
->layout
, data
->attr_list
);
247 pango_layout_set_font_description(ctx
->layout
, globalconf
.font
->desc
);
249 x
= area
.x
+ margin
->left
;
250 /* + 1 is added for rounding, so that in any case of doubt we rather draw
251 * the text 1px lower than too high which usually results in a better type
253 y
= area
.y
+ (ctx
->height
- ext
->height
+ 1) / 2 + margin
->top
;
255 /* only honors alignment if enough space */
256 if(ext
->width
< area
.width
)
260 x
+= (area
.width
- ext
->width
) / 2;
263 x
+= area
.width
- ext
->width
;
269 cairo_move_to(ctx
->cr
, x
, y
);
271 cairo_set_source_rgba(ctx
->cr
,
272 ctx
->fg
.red
/ 65535.0,
273 ctx
->fg
.green
/ 65535.0,
274 ctx
->fg
.blue
/ 65535.0,
275 ctx
->fg
.alpha
/ 65535.0);
276 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
277 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
280 /** Setup color-source for cairo (gradient or mono).
281 * \param ctx Draw context.
282 * \param gradient_vector x, y to x + x_offset, y + y_offset.
283 * \param pcolor Color to use at start of gradient_vector.
284 * \param pcolor_center Color at center of gradient_vector.
285 * \param pcolor_end Color at end of gradient_vector.
286 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
288 static cairo_pattern_t
*
289 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
290 const color_t
*pcolor
, const color_t
*pcolor_center
,
291 const color_t
*pcolor_end
)
293 cairo_pattern_t
*pat
= NULL
;
294 bool has_center
= pcolor_center
->initialized
;
295 bool has_end
= pcolor_end
->initialized
;
297 /* no need for a real pattern: */
298 if(!has_end
&& !has_center
)
299 cairo_set_source_rgba(ctx
->cr
,
301 pcolor
->green
/ 255.0,
302 pcolor
->blue
/ 255.0,
303 pcolor
->alpha
/ 255.0);
306 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
308 gradient_vector
.x
+ gradient_vector
.x_offset
,
309 gradient_vector
.y
+ gradient_vector
.y_offset
);
311 /* pcolor is always set (so far in awesome) */
312 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
314 pcolor
->green
/ 255.0,
315 pcolor
->blue
/ 255.0,
316 pcolor
->alpha
/ 255.0);
319 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
320 pcolor_center
->red
/ 255.0,
321 pcolor_center
->green
/ 255.0,
322 pcolor_center
->blue
/ 255.0,
323 pcolor_center
->alpha
/ 255.0);
326 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
327 pcolor_end
->red
/ 255.0,
328 pcolor_end
->green
/ 255.0,
329 pcolor_end
->blue
/ 255.0,
330 pcolor_end
->alpha
/ 255.0);
332 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
334 pcolor
->green
/ 255.0,
335 pcolor
->blue
/ 255.0,
336 pcolor
->alpha
/ 255.0);
337 cairo_set_source(ctx
->cr
, pat
);
342 /** Draw rectangle inside the coordinates
343 * \param ctx Draw context
344 * \param geometry geometry
345 * \param line_width line width
346 * \param filled fill rectangle?
347 * \param color color to use
350 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
351 float line_width
, bool filled
, const color_t
*color
)
353 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
354 cairo_set_line_width(ctx
->cr
, line_width
);
355 cairo_set_miter_limit(ctx
->cr
, 10.0);
356 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
357 cairo_set_source_rgba(ctx
->cr
,
359 color
->green
/ 255.0,
361 color
->alpha
/ 255.0);
364 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
365 geometry
.width
, geometry
.height
);
370 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
371 geometry
.width
- line_width
, geometry
.height
- line_width
);
372 cairo_stroke(ctx
->cr
);
376 /** Draw rectangle with gradient colors
377 * \param ctx Draw context.
378 * \param geometry Geometry.
379 * \param line_width Line width.
380 * \param filled Filled rectangle?
381 * \param gradient_vector Color-gradient course.
382 * \param pcolor Color at start of gradient_vector.
383 * \param pcolor_center Color in the center.
384 * \param pcolor_end Color at end of gradient_vector.
387 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
388 vector_t gradient_vector
, const color_t
*pcolor
,
389 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
391 cairo_pattern_t
*pat
;
393 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
394 cairo_set_line_width(ctx
->cr
, line_width
);
395 cairo_set_miter_limit(ctx
->cr
, 10.0);
396 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
398 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
402 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
407 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
408 cairo_stroke(ctx
->cr
);
412 cairo_pattern_destroy(pat
);
415 /** Setup some cairo-things for drawing a graph
416 * \param ctx Draw context
419 draw_graph_setup(draw_context_t
*ctx
)
421 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
422 cairo_set_line_width(ctx
->cr
, 1.0);
423 /* without it, it can draw over the path on sharp angles
424 * ...too long lines * (...graph_line) */
425 cairo_set_miter_limit(ctx
->cr
, 0.0);
426 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
430 * \param ctx Draw context.
431 * \param rect The area to draw into.
432 * \param from Array of starting-point offsets to draw a graph lines.
433 * \param to Array of end-point offsets to draw a graph lines.
434 * \param cur_index Current position in data-array (cycles around).
435 * \param grow Put new values to the left or to the right.
436 * \param gradient_vector Color-Gradient course.
437 * \param pcolor Color at start of gradient_vector.
438 * \param pcolor_center Color in the center.
439 * \param pcolor_end Color at end of gradient_vector.
442 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
443 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
444 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
447 float x
= rect
.x
+ 0.5; /* middle of a pixel */
448 cairo_pattern_t
*pat
;
450 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
451 pcolor
, pcolor_center
, pcolor_end
);
453 if(grow
== Right
) /* draw from right to left */
456 while(++i
< rect
.width
)
458 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
459 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
463 cur_index
= rect
.width
- 1;
466 else /* draw from left to right */
467 while(++i
< rect
.width
)
469 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
470 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
474 cur_index
= rect
.width
- 1;
477 cairo_stroke(ctx
->cr
);
480 cairo_pattern_destroy(pat
);
483 /** Draw a line into a graph-widget.
484 * \param ctx Draw context.
485 * \param rect The area to draw into.
486 * \param to array of offsets to draw the line through...
487 * \param cur_index current position in data-array (cycles around)
488 * \param grow put new values to the left or to the right
489 * \param gradient_vector Color-gradient course.
490 * \param pcolor Color at start of gradient_vector.
491 * \param pcolor_center Color in the center.
492 * \param pcolor_end Color at end of gradient_vector.
495 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
496 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
497 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
501 cairo_pattern_t
*pat
;
503 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
504 * ... it won't fill some pixels! It also looks much nicer so.
505 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
506 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
507 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
508 /* a nicer, better visible line compared to 1.0 */
509 cairo_set_line_width(ctx
->cr
, 1.25);
511 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
513 /* path through the centers of pixels */
520 /* go through the values from old to new. Begin with the oldest. */
521 if(++cur_index
> w
- 1)
524 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
527 /* on the left border: fills a pixel also when there's only one value */
528 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
530 for(i
= 0; i
< w
; i
++)
532 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
535 /* cycles around the index */
538 if(++cur_index
> w
- 1)
548 /* onto the right border: fills a pixel also when there's only one value */
550 cairo_line_to(ctx
->cr
, x
, y
- to
[(cur_index
+ (w
- 1)) % w
]);
552 cairo_stroke(ctx
->cr
);
555 cairo_pattern_destroy(pat
);
556 /* reset line-width */
557 cairo_set_line_width(ctx
->cr
, 1.0);
560 /** Draw an image from ARGB data to a draw context.
561 * Data should be stored as an array of alpha, red, blue, green for each pixel
562 * and the array size should be w * h elements long.
563 * \param ctx Draw context to draw to.
564 * \param x X coordinate.
565 * \param y Y coordinate.
568 * \param ratio The ratio to apply to the image.
569 * \param data The image pixels array.
572 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
573 double ratio
, unsigned char *data
)
576 cairo_surface_t
*source
;
578 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
579 #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)
580 sizeof(unsigned char) * 4 * w
);
582 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
584 cr
= cairo_create(ctx
->surface
);
585 cairo_scale(cr
, ratio
, ratio
);
586 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
591 cairo_surface_destroy(source
);
594 /** Draw an image to a draw context.
595 * \param ctx Draw context to draw to.
596 * \param x X coordinate.
597 * \param y Y coordinate.
598 * \param ratio The ratio to apply to the image.
599 * \param image The image to draw.
602 draw_image(draw_context_t
*ctx
, int x
, int y
, double ratio
, image_t
*image
)
604 draw_image_from_argb_data(ctx
, x
, y
, image_getwidth(image
), image_getheight(image
), ratio
, image_getdata(image
));
608 * \param ctx Draw context to draw with.
609 * \param src Drawable to draw from.
610 * \param dest Drawable to draw to.
611 * \param src_w Drawable width.
612 * \param src_h Drawable height.
613 * \param dest_w Drawable width.
614 * \param dest_h Drawable height.
615 * \param angle angle to rotate.
616 * \param tx Translate to this x coordinate.
617 * \param ty Translate to this y coordinate.
620 draw_rotate(draw_context_t
*ctx
,
621 xcb_pixmap_t src
, xcb_pixmap_t dest
,
622 int src_w
, int src_h
,
623 int dest_w
, int dest_h
,
624 double angle
, int tx
, int ty
)
626 cairo_surface_t
*surface
, *source
;
629 surface
= cairo_xcb_surface_create(globalconf
.connection
, dest
,
630 ctx
->visual
, dest_w
, dest_h
);
631 source
= cairo_xcb_surface_create(globalconf
.connection
, src
,
632 ctx
->visual
, src_w
, src_h
);
633 cr
= cairo_create (surface
);
635 cairo_translate(cr
, tx
, ty
);
636 cairo_rotate(cr
, angle
);
638 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
642 cairo_surface_destroy(source
);
643 cairo_surface_destroy(surface
);
646 /** Return the width and height of a text in pixel.
647 * \param data The draw context text data.
648 * \return Text height and width.
651 draw_text_extents(draw_text_context_t
*data
)
653 cairo_surface_t
*surface
;
657 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
658 area_t geom
= { 0, 0, 0, 0 };
663 xcb_visualtype_t
*visual
= draw_screen_default_visual(s
);
666 fatal("no visual found");
668 surface
= cairo_xcb_surface_create(globalconf
.connection
,
669 globalconf
.default_screen
,
672 s
->height_in_pixels
);
674 cr
= cairo_create(surface
);
675 layout
= pango_cairo_create_layout(cr
);
676 pango_layout_set_text(layout
, data
->text
, data
->len
);
677 pango_layout_set_attributes(layout
, data
->attr_list
);
678 pango_layout_set_font_description(layout
, globalconf
.font
->desc
);
679 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
680 g_object_unref(layout
);
682 cairo_surface_destroy(surface
);
684 geom
.width
= ext
.width
;
685 geom
.height
= ext
.height
;
690 /** Transform a string to a alignment_t type.
691 * Recognized string are flex, fixed, left, center, middle or right.
692 * Everything else will be recognized as AlignLeft.
693 * \param align Atring with align text.
694 * \param len The string length.
695 * \return An alignment_t type.
698 draw_align_fromstr(const char *align
, ssize_t len
)
700 switch(a_tokenize(align
, len
))
702 case A_TK_CENTER
: return AlignCenter
;
703 case A_TK_RIGHT
: return AlignRight
;
704 case A_TK_FLEX
: return AlignFlex
;
705 case A_TK_FIXED
: return AlignFixed
;
706 case A_TK_TOP
: return AlignTop
;
707 case A_TK_BOTTOM
: return AlignBottom
;
708 case A_TK_MIDDLE
: return AlignMiddle
;
709 default: return AlignLeft
;
713 /** Transform an alignment to a string.
714 * \param a The alignment.
715 * \return A string which must not be freed.
718 draw_align_tostr(alignment_t a
)
722 case AlignLeft
: return "left";
723 case AlignCenter
: return "center";
724 case AlignRight
: return "right";
725 case AlignFlex
: return "flex";
726 case AlignFixed
: return "fixed";
727 case AlignBottom
: return "bottom";
728 case AlignTop
: return "top";
729 case AlignMiddle
: return "middle";
730 default: return NULL
;
734 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80