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
));
85 *dlen
= orig_utf8len
- utf8len
;
90 static xcb_visualtype_t
*
91 draw_screen_default_visual(xcb_screen_t
*s
)
96 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
99 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
100 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator (depth_iter
.data
);
101 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
102 if(s
->root_visual
== visual_iter
.data
->visual_id
)
103 return visual_iter
.data
;
108 /** Create a new Pango font.
109 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
110 * \return A new font.
113 draw_font_new(const char *fontname
)
115 cairo_surface_t
*surface
;
116 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
119 font_t
*font
= p_new(font_t
, 1);
121 xcb_visualtype_t
*visual
= draw_screen_default_visual(s
);
124 fatal("cannot get visual");
126 /* Create a dummy cairo surface, cairo context and pango layout in
127 * order to get font informations */
128 surface
= cairo_xcb_surface_create(globalconf
.connection
,
129 globalconf
.default_screen
,
132 s
->height_in_pixels
);
134 cr
= cairo_create(surface
);
135 layout
= pango_cairo_create_layout(cr
);
137 /* Get the font description used to set text on a PangoLayout */
138 font
->desc
= pango_font_description_from_string(fontname
);
139 pango_layout_set_font_description(layout
, font
->desc
);
142 pango_layout_get_pixel_size(layout
, NULL
, &font
->height
);
144 g_object_unref(layout
);
146 cairo_surface_destroy(surface
);
152 * \param font Font to delete.
155 draw_font_delete(font_t
**font
)
159 pango_font_description_free((*font
)->desc
);
164 /** Initialize a draw_text_context_t with text data.
165 * \param data The draw text context to init.
166 * \param str The text string to render.
167 * \param slen The text string length.
168 * \return True if everything is ok, false otherwise.
171 draw_text_context_init(draw_text_context_t
*data
, const char *str
, ssize_t slen
)
173 GError
*error
= NULL
;
175 if(!pango_parse_markup(str
, slen
, 0, &data
->attr_list
, &data
->text
, NULL
, &error
))
177 warn("cannot parse pango markup: %s", error
? error
->message
: "unknown error");
183 data
->len
= a_strlen(data
->text
);
188 /** Initialize a new draw context.
189 * \param d The draw context to initialize.
190 * \param phys_screen Physical screen id.
191 * \param width Width.
192 * \param height Height.
193 * \param px Pixmap object to store.
194 * \param fg Foreground color.
195 * \param bg Background color.
198 draw_context_init(draw_context_t
*d
, int phys_screen
,
199 int width
, int height
, xcb_pixmap_t px
,
200 const xcolor_t
*fg
, const xcolor_t
*bg
)
202 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
204 d
->phys_screen
= phys_screen
;
207 d
->depth
= s
->root_depth
;
208 d
->visual
= draw_screen_default_visual(s
);
210 d
->surface
= cairo_xcb_surface_create(globalconf
.connection
, px
, d
->visual
, width
, height
);
211 d
->cr
= cairo_create(d
->surface
);
212 d
->layout
= pango_cairo_create_layout(d
->cr
);
217 /** Draw text into a draw context.
218 * \param ctx Draw context to draw to.
219 * \param data Draw text context data.
220 * \param elip Ellipsize mode.
221 * \param wrap Wrap mode.
222 * \param align Text alignment.
223 * \param margin Margin to respect when drawing text.
224 * \param area Area to draw to.
225 * \param ext Text extents.
228 draw_text(draw_context_t
*ctx
, draw_text_context_t
*data
,
229 PangoEllipsizeMode ellip
, PangoWrapMode wrap
,
230 alignment_t align
, padding_t
*margin
, area_t area
, area_t
*ext
)
234 pango_layout_set_text(ctx
->layout
, data
->text
, data
->len
);
235 pango_layout_set_width(ctx
->layout
,
236 pango_units_from_double(area
.width
239 pango_layout_set_height(ctx
->layout
, pango_units_from_double(area
.height
)
240 - (margin
->top
+ margin
->bottom
));
241 pango_layout_set_ellipsize(ctx
->layout
, ellip
);
242 pango_layout_set_wrap(ctx
->layout
, wrap
);
243 pango_layout_set_attributes(ctx
->layout
, data
->attr_list
);
244 pango_layout_set_font_description(ctx
->layout
, globalconf
.font
->desc
);
246 x
= area
.x
+ margin
->left
;
247 /* + 1 is added for rounding, so that in any case of doubt we rather draw
248 * the text 1px lower than too high which usually results in a better type
250 y
= area
.y
+ (ctx
->height
- ext
->height
+ 1) / 2 + margin
->top
;
252 /* only honors alignment if enough space */
253 if(ext
->width
< area
.width
)
257 x
+= (area
.width
- ext
->width
) / 2;
260 x
+= area
.width
- ext
->width
;
266 cairo_move_to(ctx
->cr
, x
, y
);
268 cairo_set_source_rgba(ctx
->cr
,
269 ctx
->fg
.red
/ 65535.0,
270 ctx
->fg
.green
/ 65535.0,
271 ctx
->fg
.blue
/ 65535.0,
272 ctx
->fg
.alpha
/ 65535.0);
273 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
274 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
277 /** Setup color-source for cairo (gradient or mono).
278 * \param ctx Draw context.
279 * \param gradient_vector x, y to x + x_offset, y + y_offset.
280 * \param pcolor Color to use at start of gradient_vector.
281 * \param pcolor_center Color at center of gradient_vector.
282 * \param pcolor_end Color at end of gradient_vector.
283 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
285 static cairo_pattern_t
*
286 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
287 const color_t
*pcolor
, const color_t
*pcolor_center
,
288 const color_t
*pcolor_end
)
290 cairo_pattern_t
*pat
= NULL
;
291 bool has_center
= pcolor_center
->initialized
;
292 bool has_end
= pcolor_end
->initialized
;
294 /* no need for a real pattern: */
295 if(!has_end
&& !has_center
)
296 cairo_set_source_rgba(ctx
->cr
,
298 pcolor
->green
/ 255.0,
299 pcolor
->blue
/ 255.0,
300 pcolor
->alpha
/ 255.0);
303 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
305 gradient_vector
.x
+ gradient_vector
.x_offset
,
306 gradient_vector
.y
+ gradient_vector
.y_offset
);
308 /* pcolor is always set (so far in awesome) */
309 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
311 pcolor
->green
/ 255.0,
312 pcolor
->blue
/ 255.0,
313 pcolor
->alpha
/ 255.0);
316 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
317 pcolor_center
->red
/ 255.0,
318 pcolor_center
->green
/ 255.0,
319 pcolor_center
->blue
/ 255.0,
320 pcolor_center
->alpha
/ 255.0);
323 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
324 pcolor_end
->red
/ 255.0,
325 pcolor_end
->green
/ 255.0,
326 pcolor_end
->blue
/ 255.0,
327 pcolor_end
->alpha
/ 255.0);
329 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
331 pcolor
->green
/ 255.0,
332 pcolor
->blue
/ 255.0,
333 pcolor
->alpha
/ 255.0);
334 cairo_set_source(ctx
->cr
, pat
);
339 /** Draw rectangle inside the coordinates
340 * \param ctx Draw context
341 * \param geometry geometry
342 * \param line_width line width
343 * \param filled fill rectangle?
344 * \param color color to use
347 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
348 float line_width
, bool filled
, const color_t
*color
)
350 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
351 cairo_set_line_width(ctx
->cr
, line_width
);
352 cairo_set_miter_limit(ctx
->cr
, 10.0);
353 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
354 cairo_set_source_rgba(ctx
->cr
,
356 color
->green
/ 255.0,
358 color
->alpha
/ 255.0);
361 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
362 geometry
.width
, geometry
.height
);
367 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
368 geometry
.width
- line_width
, geometry
.height
- line_width
);
369 cairo_stroke(ctx
->cr
);
373 /** Draw rectangle with gradient colors
374 * \param ctx Draw context.
375 * \param geometry Geometry.
376 * \param line_width Line width.
377 * \param filled Filled rectangle?
378 * \param gradient_vector Color-gradient course.
379 * \param pcolor Color at start of gradient_vector.
380 * \param pcolor_center Color in the center.
381 * \param pcolor_end Color at end of gradient_vector.
384 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
385 vector_t gradient_vector
, const color_t
*pcolor
,
386 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
388 cairo_pattern_t
*pat
;
390 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
391 cairo_set_line_width(ctx
->cr
, line_width
);
392 cairo_set_miter_limit(ctx
->cr
, 10.0);
393 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
395 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
399 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
404 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
405 cairo_stroke(ctx
->cr
);
409 cairo_pattern_destroy(pat
);
412 /** Setup some cairo-things for drawing a graph
413 * \param ctx Draw context
416 draw_graph_setup(draw_context_t
*ctx
)
418 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
419 cairo_set_line_width(ctx
->cr
, 1.0);
420 /* without it, it can draw over the path on sharp angles
421 * ...too long lines * (...graph_line) */
422 cairo_set_miter_limit(ctx
->cr
, 0.0);
423 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
427 * \param ctx Draw context.
428 * \param rect The area to draw into.
429 * \param from Array of starting-point offsets to draw a graph lines.
430 * \param to Array of end-point offsets to draw a graph lines.
431 * \param cur_index Current position in data-array (cycles around).
432 * \param grow Put new values to the left or to the right.
433 * \param gradient_vector Color-Gradient course.
434 * \param pcolor Color at start of gradient_vector.
435 * \param pcolor_center Color in the center.
436 * \param pcolor_end Color at end of gradient_vector.
439 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
440 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
441 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
444 float x
= rect
.x
+ 0.5; /* middle of a pixel */
445 cairo_pattern_t
*pat
;
447 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
448 pcolor
, pcolor_center
, pcolor_end
);
450 if(grow
== Right
) /* draw from right to left */
453 while(++i
< rect
.width
)
455 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
456 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
460 cur_index
= rect
.width
- 1;
463 else /* draw from left to right */
464 while(++i
< rect
.width
)
466 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
467 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
471 cur_index
= rect
.width
- 1;
474 cairo_stroke(ctx
->cr
);
477 cairo_pattern_destroy(pat
);
480 /** Draw a line into a graph-widget.
481 * \param ctx Draw context.
482 * \param rect The area to draw into.
483 * \param to array of offsets to draw the line through...
484 * \param cur_index current position in data-array (cycles around)
485 * \param grow put new values to the left or to the right
486 * \param gradient_vector Color-gradient course.
487 * \param pcolor Color at start of gradient_vector.
488 * \param pcolor_center Color in the center.
489 * \param pcolor_end Color at end of gradient_vector.
492 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
493 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
494 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
498 cairo_pattern_t
*pat
;
500 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
501 * ... it won't fill some pixels! It also looks much nicer so.
502 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
503 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
504 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
505 /* a nicer, better visible line compared to 1.0 */
506 cairo_set_line_width(ctx
->cr
, 1.25);
508 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
510 /* path through the centers of pixels */
517 /* go through the values from old to new. Begin with the oldest. */
518 if(++cur_index
> w
- 1)
521 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
524 /* on the left border: fills a pixel also when there's only one value */
525 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
527 for(i
= 0; i
< w
; i
++)
529 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
532 /* cycles around the index */
535 if(++cur_index
> w
- 1)
545 /* onto the right border: fills a pixel also when there's only one value */
547 cairo_line_to(ctx
->cr
, x
, y
- to
[(cur_index
+ (w
- 1)) % w
]);
549 cairo_stroke(ctx
->cr
);
552 cairo_pattern_destroy(pat
);
553 /* reset line-width */
554 cairo_set_line_width(ctx
->cr
, 1.0);
557 /** Draw an image from ARGB data to a draw context.
558 * Data should be stored as an array of alpha, red, blue, green for each pixel
559 * and the array size should be w * h elements long.
560 * \param ctx Draw context to draw to.
561 * \param x X coordinate.
562 * \param y Y coordinate.
565 * \param ratio The ratio to apply to the image.
566 * \param data The image pixels array.
569 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
570 double ratio
, unsigned char *data
)
573 cairo_surface_t
*source
;
575 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
576 #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)
577 sizeof(unsigned char) * 4 * w
);
579 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
581 cr
= cairo_create(ctx
->surface
);
582 cairo_scale(cr
, ratio
, ratio
);
583 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
588 cairo_surface_destroy(source
);
591 /** Draw an image to a draw context.
592 * \param ctx Draw context to draw to.
593 * \param x X coordinate.
594 * \param y Y coordinate.
595 * \param ratio The ratio to apply to the image.
596 * \param image The image to draw.
599 draw_image(draw_context_t
*ctx
, int x
, int y
, double ratio
, image_t
*image
)
601 draw_image_from_argb_data(ctx
, x
, y
, image_getwidth(image
), image_getheight(image
), ratio
, image_getdata(image
));
605 * \param ctx Draw context to draw with.
606 * \param src Drawable to draw from.
607 * \param dest Drawable to draw to.
608 * \param src_w Drawable width.
609 * \param src_h Drawable height.
610 * \param dest_w Drawable width.
611 * \param dest_h Drawable height.
612 * \param angle angle to rotate.
613 * \param tx Translate to this x coordinate.
614 * \param ty Translate to this y coordinate.
617 draw_rotate(draw_context_t
*ctx
,
618 xcb_pixmap_t src
, xcb_pixmap_t dest
,
619 int src_w
, int src_h
,
620 int dest_w
, int dest_h
,
621 double angle
, int tx
, int ty
)
623 cairo_surface_t
*surface
, *source
;
626 surface
= cairo_xcb_surface_create(globalconf
.connection
, dest
,
627 ctx
->visual
, dest_w
, dest_h
);
628 source
= cairo_xcb_surface_create(globalconf
.connection
, src
,
629 ctx
->visual
, src_w
, src_h
);
630 cr
= cairo_create (surface
);
632 cairo_translate(cr
, tx
, ty
);
633 cairo_rotate(cr
, angle
);
635 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
639 cairo_surface_destroy(source
);
640 cairo_surface_destroy(surface
);
643 /** Return the width and height of a text in pixel.
644 * \param data The draw context text data.
645 * \return Text height and width.
648 draw_text_extents(draw_text_context_t
*data
)
650 cairo_surface_t
*surface
;
654 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
655 area_t geom
= { 0, 0, 0, 0 };
660 xcb_visualtype_t
*visual
= draw_screen_default_visual(s
);
663 fatal("no visual found");
665 surface
= cairo_xcb_surface_create(globalconf
.connection
,
666 globalconf
.default_screen
,
669 s
->height_in_pixels
);
671 cr
= cairo_create(surface
);
672 layout
= pango_cairo_create_layout(cr
);
673 pango_layout_set_text(layout
, data
->text
, data
->len
);
674 pango_layout_set_attributes(layout
, data
->attr_list
);
675 pango_layout_set_font_description(layout
, globalconf
.font
->desc
);
676 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
677 g_object_unref(layout
);
679 cairo_surface_destroy(surface
);
681 geom
.width
= ext
.width
;
682 geom
.height
= ext
.height
;
687 /** Transform a string to a alignment_t type.
688 * Recognized string are flex, fixed, left, center, middle or right.
689 * Everything else will be recognized as AlignLeft.
690 * \param align Atring with align text.
691 * \param len The string length.
692 * \return An alignment_t type.
695 draw_align_fromstr(const char *align
, ssize_t len
)
697 switch(a_tokenize(align
, len
))
699 case A_TK_CENTER
: return AlignCenter
;
700 case A_TK_RIGHT
: return AlignRight
;
701 case A_TK_FLEX
: return AlignFlex
;
702 case A_TK_FIXED
: return AlignFixed
;
703 case A_TK_TOP
: return AlignTop
;
704 case A_TK_BOTTOM
: return AlignBottom
;
705 case A_TK_MIDDLE
: return AlignMiddle
;
706 default: return AlignLeft
;
710 /** Transform an alignment to a string.
711 * \param a The alignment.
712 * \return A string which must not be freed.
715 draw_align_tostr(alignment_t a
)
719 case AlignLeft
: return "left";
720 case AlignCenter
: return "center";
721 case AlignRight
: return "right";
722 case AlignFlex
: return "flex";
723 case AlignFixed
: return "fixed";
724 case AlignBottom
: return "bottom";
725 case AlignTop
: return "top";
726 case AlignMiddle
: return "middle";
727 default: return NULL
;
731 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80