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
->visual
= draw_screen_default_visual(s
);
209 d
->surface
= cairo_xcb_surface_create(globalconf
.connection
, px
, d
->visual
, width
, height
);
210 d
->cr
= cairo_create(d
->surface
);
211 d
->layout
= pango_cairo_create_layout(d
->cr
);
216 /** Draw text into a draw context.
217 * \param ctx Draw context to draw to.
218 * \param data Draw text context data.
219 * \param elip Ellipsize mode.
220 * \param wrap Wrap mode.
221 * \param align Text alignment.
222 * \param margin Margin to respect when drawing text.
223 * \param area Area to draw to.
224 * \param ext Text extents.
227 draw_text(draw_context_t
*ctx
, draw_text_context_t
*data
,
228 PangoEllipsizeMode ellip
, PangoWrapMode wrap
,
229 alignment_t align
, padding_t
*margin
, area_t area
, area_t
*ext
)
233 pango_layout_set_text(ctx
->layout
, data
->text
, data
->len
);
234 pango_layout_set_width(ctx
->layout
,
235 pango_units_from_double(area
.width
238 pango_layout_set_height(ctx
->layout
, pango_units_from_double(area
.height
)
239 - (margin
->top
+ margin
->bottom
));
240 pango_layout_set_ellipsize(ctx
->layout
, ellip
);
241 pango_layout_set_wrap(ctx
->layout
, wrap
);
242 pango_layout_set_attributes(ctx
->layout
, data
->attr_list
);
243 pango_layout_set_font_description(ctx
->layout
, globalconf
.font
->desc
);
245 x
= area
.x
+ margin
->left
;
246 /* + 1 is added for rounding, so that in any case of doubt we rather draw
247 * the text 1px lower than too high which usually results in a better type
249 y
= area
.y
+ (ctx
->height
- ext
->height
+ 1) / 2 + margin
->top
;
251 /* only honors alignment if enough space */
252 if(ext
->width
< area
.width
)
256 x
+= (area
.width
- ext
->width
) / 2;
259 x
+= area
.width
- ext
->width
;
265 cairo_move_to(ctx
->cr
, x
, y
);
267 cairo_set_source_rgba(ctx
->cr
,
268 ctx
->fg
.red
/ 65535.0,
269 ctx
->fg
.green
/ 65535.0,
270 ctx
->fg
.blue
/ 65535.0,
271 ctx
->fg
.alpha
/ 65535.0);
272 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
273 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
276 /** Setup color-source for cairo (gradient or mono).
277 * \param ctx Draw context.
278 * \param gradient_vector x, y to x + x_offset, y + y_offset.
279 * \param pcolor Color to use at start of gradient_vector.
280 * \param pcolor_center Color at center of gradient_vector.
281 * \param pcolor_end Color at end of gradient_vector.
282 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
284 static cairo_pattern_t
*
285 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
286 const color_t
*pcolor
, const color_t
*pcolor_center
,
287 const color_t
*pcolor_end
)
289 cairo_pattern_t
*pat
= NULL
;
290 bool has_center
= pcolor_center
->initialized
;
291 bool has_end
= pcolor_end
->initialized
;
293 /* no need for a real pattern: */
294 if(!has_end
&& !has_center
)
295 cairo_set_source_rgba(ctx
->cr
,
297 pcolor
->green
/ 255.0,
298 pcolor
->blue
/ 255.0,
299 pcolor
->alpha
/ 255.0);
302 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
304 gradient_vector
.x
+ gradient_vector
.x_offset
,
305 gradient_vector
.y
+ gradient_vector
.y_offset
);
307 /* pcolor is always set (so far in awesome) */
308 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
310 pcolor
->green
/ 255.0,
311 pcolor
->blue
/ 255.0,
312 pcolor
->alpha
/ 255.0);
315 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
316 pcolor_center
->red
/ 255.0,
317 pcolor_center
->green
/ 255.0,
318 pcolor_center
->blue
/ 255.0,
319 pcolor_center
->alpha
/ 255.0);
322 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
323 pcolor_end
->red
/ 255.0,
324 pcolor_end
->green
/ 255.0,
325 pcolor_end
->blue
/ 255.0,
326 pcolor_end
->alpha
/ 255.0);
328 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
330 pcolor
->green
/ 255.0,
331 pcolor
->blue
/ 255.0,
332 pcolor
->alpha
/ 255.0);
333 cairo_set_source(ctx
->cr
, pat
);
338 /** Draw rectangle inside the coordinates
339 * \param ctx Draw context
340 * \param geometry geometry
341 * \param line_width line width
342 * \param filled fill rectangle?
343 * \param color color to use
346 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
347 float line_width
, bool filled
, const color_t
*color
)
349 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
350 cairo_set_line_width(ctx
->cr
, line_width
);
351 cairo_set_miter_limit(ctx
->cr
, 10.0);
352 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
353 cairo_set_source_rgba(ctx
->cr
,
355 color
->green
/ 255.0,
357 color
->alpha
/ 255.0);
360 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
361 geometry
.width
, geometry
.height
);
366 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
367 geometry
.width
- line_width
, geometry
.height
- line_width
);
368 cairo_stroke(ctx
->cr
);
372 /** Draw rectangle with gradient colors
373 * \param ctx Draw context.
374 * \param geometry Geometry.
375 * \param line_width Line width.
376 * \param filled Filled rectangle?
377 * \param gradient_vector Color-gradient course.
378 * \param pcolor Color at start of gradient_vector.
379 * \param pcolor_center Color in the center.
380 * \param pcolor_end Color at end of gradient_vector.
383 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
384 vector_t gradient_vector
, const color_t
*pcolor
,
385 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
387 cairo_pattern_t
*pat
;
389 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
390 cairo_set_line_width(ctx
->cr
, line_width
);
391 cairo_set_miter_limit(ctx
->cr
, 10.0);
392 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
394 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
398 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
403 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
404 cairo_stroke(ctx
->cr
);
408 cairo_pattern_destroy(pat
);
411 /** Setup some cairo-things for drawing a graph
412 * \param ctx Draw context
415 draw_graph_setup(draw_context_t
*ctx
)
417 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
418 cairo_set_line_width(ctx
->cr
, 1.0);
419 /* without it, it can draw over the path on sharp angles
420 * ...too long lines * (...graph_line) */
421 cairo_set_miter_limit(ctx
->cr
, 0.0);
422 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
426 * \param ctx Draw context.
427 * \param rect The area to draw into.
428 * \param from Array of starting-point offsets to draw a graph lines.
429 * \param to Array of end-point offsets to draw a graph lines.
430 * \param cur_index Current position in data-array (cycles around).
431 * \param grow Put new values to the left or to the right.
432 * \param gradient_vector Color-Gradient course.
433 * \param pcolor Color at start of gradient_vector.
434 * \param pcolor_center Color in the center.
435 * \param pcolor_end Color at end of gradient_vector.
438 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
439 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
440 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
443 float x
= rect
.x
+ 0.5; /* middle of a pixel */
444 cairo_pattern_t
*pat
;
446 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
447 pcolor
, pcolor_center
, pcolor_end
);
449 if(grow
== Right
) /* draw from right to left */
452 while(++i
< rect
.width
)
454 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
455 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
459 cur_index
= rect
.width
- 1;
462 else /* draw from left to right */
463 while(++i
< rect
.width
)
465 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
466 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
470 cur_index
= rect
.width
- 1;
473 cairo_stroke(ctx
->cr
);
476 cairo_pattern_destroy(pat
);
479 /** Draw a line into a graph-widget.
480 * \param ctx Draw context.
481 * \param rect The area to draw into.
482 * \param to array of offsets to draw the line through...
483 * \param cur_index current position in data-array (cycles around)
484 * \param grow put new values to the left or to the right
485 * \param gradient_vector Color-gradient course.
486 * \param pcolor Color at start of gradient_vector.
487 * \param pcolor_center Color in the center.
488 * \param pcolor_end Color at end of gradient_vector.
491 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
492 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
493 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
497 cairo_pattern_t
*pat
;
499 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
500 * ... it won't fill some pixels! It also looks much nicer so.
501 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
502 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
503 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
504 /* a nicer, better visible line compared to 1.0 */
505 cairo_set_line_width(ctx
->cr
, 1.25);
507 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
509 /* path through the centers of pixels */
516 /* go through the values from old to new. Begin with the oldest. */
517 if(++cur_index
> w
- 1)
520 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
523 /* on the left border: fills a pixel also when there's only one value */
524 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
526 for(i
= 0; i
< w
; i
++)
528 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
531 /* cycles around the index */
534 if(++cur_index
> w
- 1)
544 /* onto the right border: fills a pixel also when there's only one value */
546 cairo_line_to(ctx
->cr
, x
, y
- to
[(cur_index
+ (w
- 1)) % w
]);
548 cairo_stroke(ctx
->cr
);
551 cairo_pattern_destroy(pat
);
552 /* reset line-width */
553 cairo_set_line_width(ctx
->cr
, 1.0);
556 /** Draw an image from ARGB data to a draw context.
557 * Data should be stored as an array of alpha, red, blue, green for each pixel
558 * and the array size should be w * h elements long.
559 * \param ctx Draw context to draw to.
560 * \param x X coordinate.
561 * \param y Y coordinate.
564 * \param ratio The ratio to apply to the image.
565 * \param data The image pixels array.
568 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
569 double ratio
, unsigned char *data
)
572 cairo_surface_t
*source
;
574 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
575 #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)
576 sizeof(unsigned char) * 4 * w
);
578 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
580 cr
= cairo_create(ctx
->surface
);
581 cairo_scale(cr
, ratio
, ratio
);
582 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
587 cairo_surface_destroy(source
);
590 /** Draw an image to a draw context.
591 * \param ctx Draw context to draw to.
592 * \param x X coordinate.
593 * \param y Y coordinate.
594 * \param ratio The ratio to apply to the image.
595 * \param image The image to draw.
598 draw_image(draw_context_t
*ctx
, int x
, int y
, double ratio
, image_t
*image
)
600 draw_image_from_argb_data(ctx
, x
, y
, image_getwidth(image
), image_getheight(image
), ratio
, image_getdata(image
));
604 * \param ctx Draw context to draw with.
605 * \param src Drawable to draw from.
606 * \param dest Drawable to draw to.
607 * \param src_w Drawable width.
608 * \param src_h Drawable height.
609 * \param dest_w Drawable width.
610 * \param dest_h Drawable height.
611 * \param angle angle to rotate.
612 * \param tx Translate to this x coordinate.
613 * \param ty Translate to this y coordinate.
616 draw_rotate(draw_context_t
*ctx
,
617 xcb_pixmap_t src
, xcb_pixmap_t dest
,
618 int src_w
, int src_h
,
619 int dest_w
, int dest_h
,
620 double angle
, int tx
, int ty
)
622 cairo_surface_t
*surface
, *source
;
625 surface
= cairo_xcb_surface_create(globalconf
.connection
, dest
,
626 ctx
->visual
, dest_w
, dest_h
);
627 source
= cairo_xcb_surface_create(globalconf
.connection
, src
,
628 ctx
->visual
, src_w
, src_h
);
629 cr
= cairo_create (surface
);
631 cairo_translate(cr
, tx
, ty
);
632 cairo_rotate(cr
, angle
);
634 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
638 cairo_surface_destroy(source
);
639 cairo_surface_destroy(surface
);
642 /** Return the width and height of a text in pixel.
643 * \param data The draw context text data.
644 * \return Text height and width.
647 draw_text_extents(draw_text_context_t
*data
)
649 cairo_surface_t
*surface
;
653 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
654 area_t geom
= { 0, 0, 0, 0 };
659 xcb_visualtype_t
*visual
= draw_screen_default_visual(s
);
662 fatal("no visual found");
664 surface
= cairo_xcb_surface_create(globalconf
.connection
,
665 globalconf
.default_screen
,
668 s
->height_in_pixels
);
670 cr
= cairo_create(surface
);
671 layout
= pango_cairo_create_layout(cr
);
672 pango_layout_set_text(layout
, data
->text
, data
->len
);
673 pango_layout_set_attributes(layout
, data
->attr_list
);
674 pango_layout_set_font_description(layout
, globalconf
.font
->desc
);
675 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
676 g_object_unref(layout
);
678 cairo_surface_destroy(surface
);
680 geom
.width
= ext
.width
;
681 geom
.height
= ext
.height
;
686 /** Transform a string to a alignment_t type.
687 * Recognized string are flex, fixed, left, center, middle or right.
688 * Everything else will be recognized as AlignLeft.
689 * \param align Atring with align text.
690 * \param len The string length.
691 * \return An alignment_t type.
694 draw_align_fromstr(const char *align
, ssize_t len
)
696 switch(a_tokenize(align
, len
))
698 case A_TK_CENTER
: return AlignCenter
;
699 case A_TK_RIGHT
: return AlignRight
;
700 case A_TK_FLEX
: return AlignFlex
;
701 case A_TK_FIXED
: return AlignFixed
;
702 case A_TK_TOP
: return AlignTop
;
703 case A_TK_BOTTOM
: return AlignBottom
;
704 case A_TK_MIDDLE
: return AlignMiddle
;
705 default: return AlignLeft
;
709 /** Transform an alignment to a string.
710 * \param a The alignment.
711 * \return A string which must not be freed.
714 draw_align_tostr(alignment_t a
)
718 case AlignLeft
: return "left";
719 case AlignCenter
: return "center";
720 case AlignRight
: return "right";
721 case AlignFlex
: return "flex";
722 case AlignFixed
: return "fixed";
723 case AlignBottom
: return "bottom";
724 case AlignTop
: return "top";
725 case AlignMiddle
: return "middle";
726 default: return NULL
;
730 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80