2 * draw.c - draw functions
4 * Copyright © 2007-2009 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>
35 #include "common/tokenize.h"
36 #include "common/xutil.h"
38 /** Convert text from any charset to UTF-8 using iconv.
39 * \param iso The ISO string to convert.
40 * \param len The string size.
41 * \param dest The destination pointer. Memory will be allocated, up to you to
42 * free, like any char *.
43 * \param dlen The destination length, can be NULL.
44 * \return True if conversion was done.
47 draw_iso2utf8(const char *iso
, size_t len
, char **dest
, ssize_t
*dlen
)
49 static iconv_t iso2utf8
= (iconv_t
) -1;
50 static int8_t dont_need_convert
= -1;
52 if(dont_need_convert
== -1)
53 dont_need_convert
= !a_strcmp(nl_langinfo(CODESET
), "UTF-8");
55 if(!len
|| dont_need_convert
)
58 if(iso2utf8
== (iconv_t
) -1)
60 iso2utf8
= iconv_open("UTF-8", nl_langinfo(CODESET
));
61 if(iso2utf8
== (iconv_t
) -1)
64 warn("unable to convert text from %s to UTF-8, not available",
65 nl_langinfo(CODESET
));
67 warn("unable to convert text: %s", strerror(errno
));
73 size_t orig_utf8len
, utf8len
;
76 orig_utf8len
= utf8len
= 2 * len
+ 1;
77 utf8
= *dest
= p_new(char, utf8len
);
79 if(iconv(iso2utf8
, (char **) &iso
, &len
, &utf8
, &utf8len
) == (size_t) -1)
81 warn("text conversion failed: %s", strerror(errno
));
87 *dlen
= orig_utf8len
- utf8len
;
92 /** Create a new Pango font.
93 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
97 draw_font_new(const char *fontname
)
99 cairo_surface_t
*surface
;
100 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
103 font_t
*font
= p_new(font_t
, 1);
105 /* Create a dummy cairo surface, cairo context and pango layout in
106 * order to get font informations */
107 surface
= cairo_xcb_surface_create(globalconf
.connection
,
108 globalconf
.default_screen
,
109 globalconf
.screens
.tab
[0].visual
,
111 s
->height_in_pixels
);
113 cr
= cairo_create(surface
);
114 layout
= pango_cairo_create_layout(cr
);
116 /* Get the font description used to set text on a PangoLayout */
117 font
->desc
= pango_font_description_from_string(fontname
);
118 pango_layout_set_font_description(layout
, font
->desc
);
121 pango_layout_get_pixel_size(layout
, NULL
, &font
->height
);
123 g_object_unref(layout
);
125 cairo_surface_destroy(surface
);
131 * \param font Font to delete.
134 draw_font_delete(font_t
**font
)
138 pango_font_description_free((*font
)->desc
);
143 /** Initialize a draw_text_context_t with text data.
144 * \param data The draw text context to init.
145 * \param str The text string to render.
146 * \param slen The text string length.
147 * \return True if everything is ok, false otherwise.
150 draw_text_context_init(draw_text_context_t
*data
, const char *str
, ssize_t slen
)
152 GError
*error
= NULL
;
157 if(!pango_parse_markup(str
, slen
, 0, &data
->attr_list
, &data
->text
, NULL
, &error
))
159 warn("cannot parse pango markup: %s", error
? error
->message
: "unknown error");
165 data
->len
= a_strlen(data
->text
);
170 /** Initialize a new draw context.
171 * \param d The draw context to initialize.
172 * \param phys_screen Physical screen id.
173 * \param width Width.
174 * \param height Height.
175 * \param px Pixmap object to store.
176 * \param fg Foreground color.
177 * \param bg Background color.
180 draw_context_init(draw_context_t
*d
, int phys_screen
,
181 int width
, int height
, xcb_pixmap_t px
,
182 const xcolor_t
*fg
, const xcolor_t
*bg
)
184 d
->phys_screen
= phys_screen
;
188 d
->surface
= cairo_xcb_surface_create(globalconf
.connection
,
189 px
, globalconf
.screens
.tab
[phys_screen
].visual
,
191 d
->cr
= cairo_create(d
->surface
);
192 d
->layout
= pango_cairo_create_layout(d
->cr
);
197 /** Draw text into a draw context.
198 * \param ctx Draw context to draw to.
199 * \param data Draw text context data.
200 * \param ellip Ellipsize mode.
201 * \param wrap Wrap mode.
202 * \param align Text alignment.
203 * \param margin Margin to respect when drawing text.
204 * \param area Area to draw to.
205 * \param ext Text extents.
208 draw_text(draw_context_t
*ctx
, draw_text_context_t
*data
,
209 PangoEllipsizeMode ellip
, PangoWrapMode wrap
,
210 alignment_t align
, padding_t
*margin
, area_t area
, area_t
*ext
)
214 pango_layout_set_text(ctx
->layout
, data
->text
, data
->len
);
215 pango_layout_set_width(ctx
->layout
,
216 pango_units_from_double(area
.width
219 pango_layout_set_height(ctx
->layout
, pango_units_from_double(area
.height
)
220 - (margin
->top
+ margin
->bottom
));
221 pango_layout_set_ellipsize(ctx
->layout
, ellip
);
222 pango_layout_set_wrap(ctx
->layout
, wrap
);
223 pango_layout_set_attributes(ctx
->layout
, data
->attr_list
);
224 pango_layout_set_font_description(ctx
->layout
, globalconf
.font
->desc
);
226 x
= area
.x
+ margin
->left
;
227 y
= area
.y
+ margin
->top
;
229 /* only honors alignment if enough space */
230 if(ext
->width
< area
.width
)
234 x
+= (area
.width
- ext
->width
) / 2;
237 x
+= area
.width
- ext
->width
;
243 cairo_move_to(ctx
->cr
, x
, y
);
245 cairo_set_source_rgba(ctx
->cr
,
246 ctx
->fg
.red
/ 65535.0,
247 ctx
->fg
.green
/ 65535.0,
248 ctx
->fg
.blue
/ 65535.0,
249 ctx
->fg
.alpha
/ 65535.0);
250 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
251 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
254 /** Setup color-source for cairo (gradient or mono).
255 * \param ctx Draw context.
256 * \param gradient_vector x, y to x + x_offset, y + y_offset.
257 * \param pcolor Color to use at start of gradient_vector.
258 * \param pcolor_center Color at center of gradient_vector.
259 * \param pcolor_end Color at end of gradient_vector.
260 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
262 static cairo_pattern_t
*
263 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
264 const color_t
*pcolor
, const color_t
*pcolor_center
,
265 const color_t
*pcolor_end
)
267 cairo_pattern_t
*pat
= NULL
;
268 bool has_center
= pcolor_center
->initialized
;
269 bool has_end
= pcolor_end
->initialized
;
271 /* no need for a real pattern: */
272 if(!has_end
&& !has_center
)
273 cairo_set_source_rgba(ctx
->cr
,
275 pcolor
->green
/ 255.0,
276 pcolor
->blue
/ 255.0,
277 pcolor
->alpha
/ 255.0);
280 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
282 gradient_vector
.x
+ gradient_vector
.x_offset
,
283 gradient_vector
.y
+ gradient_vector
.y_offset
);
285 /* pcolor is always set (so far in awesome) */
286 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
288 pcolor
->green
/ 255.0,
289 pcolor
->blue
/ 255.0,
290 pcolor
->alpha
/ 255.0);
293 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
294 pcolor_center
->red
/ 255.0,
295 pcolor_center
->green
/ 255.0,
296 pcolor_center
->blue
/ 255.0,
297 pcolor_center
->alpha
/ 255.0);
300 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
301 pcolor_end
->red
/ 255.0,
302 pcolor_end
->green
/ 255.0,
303 pcolor_end
->blue
/ 255.0,
304 pcolor_end
->alpha
/ 255.0);
306 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
308 pcolor
->green
/ 255.0,
309 pcolor
->blue
/ 255.0,
310 pcolor
->alpha
/ 255.0);
311 cairo_set_source(ctx
->cr
, pat
);
316 /** Draw rectangle inside the coordinates
317 * \param ctx Draw context
318 * \param geometry geometry
319 * \param line_width line width
320 * \param filled fill rectangle?
321 * \param color color to use
324 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
325 float line_width
, bool filled
, const color_t
*color
)
327 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
328 cairo_set_line_width(ctx
->cr
, line_width
);
329 cairo_set_miter_limit(ctx
->cr
, 10.0);
330 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
331 cairo_set_source_rgba(ctx
->cr
,
333 color
->green
/ 255.0,
335 color
->alpha
/ 255.0);
338 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
339 geometry
.width
, geometry
.height
);
344 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
345 geometry
.width
- line_width
, geometry
.height
- line_width
);
346 cairo_stroke(ctx
->cr
);
350 /** Draw rectangle with gradient colors
351 * \param ctx Draw context.
352 * \param geometry Geometry.
353 * \param line_width Line width.
354 * \param filled Filled rectangle?
355 * \param gradient_vector Color-gradient course.
356 * \param pcolor Color at start of gradient_vector.
357 * \param pcolor_center Color in the center.
358 * \param pcolor_end Color at end of gradient_vector.
361 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
362 vector_t gradient_vector
, const color_t
*pcolor
,
363 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
365 cairo_pattern_t
*pat
;
367 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
368 cairo_set_line_width(ctx
->cr
, line_width
);
369 cairo_set_miter_limit(ctx
->cr
, 10.0);
370 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
372 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
376 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
381 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
382 cairo_stroke(ctx
->cr
);
386 cairo_pattern_destroy(pat
);
389 /** Setup some cairo-things for drawing a graph
390 * \param ctx Draw context
393 draw_graph_setup(draw_context_t
*ctx
)
395 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
396 cairo_set_line_width(ctx
->cr
, 1.0);
397 /* without it, it can draw over the path on sharp angles
398 * ...too long lines * (...graph_line) */
399 cairo_set_miter_limit(ctx
->cr
, 0.0);
400 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
404 * \param ctx Draw context.
405 * \param rect The area to draw into.
406 * \param from Array of starting-point offsets to draw a graph lines.
407 * \param to Array of end-point offsets to draw a graph lines.
408 * \param cur_index Current position in data-array (cycles around).
409 * \param grow Put new values to the left or to the right.
410 * \param gradient_vector Color-Gradient course.
411 * \param pcolor Color at start of gradient_vector.
412 * \param pcolor_center Color in the center.
413 * \param pcolor_end Color at end of gradient_vector.
416 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
417 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
418 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
421 float x
= rect
.x
+ 0.5; /* middle of a pixel */
422 cairo_pattern_t
*pat
;
424 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
425 pcolor
, pcolor_center
, pcolor_end
);
427 if(grow
== Right
) /* draw from right to left */
430 while(++i
< rect
.width
)
432 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
433 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
437 cur_index
= rect
.width
- 1;
440 else /* draw from left to right */
441 while(++i
< rect
.width
)
443 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
444 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
448 cur_index
= rect
.width
- 1;
451 cairo_stroke(ctx
->cr
);
454 cairo_pattern_destroy(pat
);
457 /** Draw a line into a graph-widget.
458 * \param ctx Draw context.
459 * \param rect The area to draw into.
460 * \param to array of offsets to draw the line through...
461 * \param cur_index current position in data-array (cycles around)
462 * \param grow put new values to the left or to the right
463 * \param gradient_vector Color-gradient course.
464 * \param pcolor Color at start of gradient_vector.
465 * \param pcolor_center Color in the center.
466 * \param pcolor_end Color at end of gradient_vector.
469 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
470 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
471 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
475 cairo_pattern_t
*pat
;
477 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
478 * ... it won't fill some pixels! It also looks much nicer so.
479 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
480 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
481 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
482 /* a nicer, better visible line compared to 1.0 */
483 cairo_set_line_width(ctx
->cr
, 1.25);
485 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
487 /* path through the centers of pixels */
494 /* go through the values from old to new. Begin with the oldest. */
495 if(++cur_index
> w
- 1)
498 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
501 /* on the left border: fills a pixel also when there's only one value */
502 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
504 for(i
= 0; i
< w
; i
++)
506 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
509 /* cycles around the index */
512 if(++cur_index
> w
- 1)
522 /* onto the right border: fills a pixel also when there's only one value */
524 cairo_line_to(ctx
->cr
, x
, y
- to
[(cur_index
+ (w
- 1)) % w
]);
526 cairo_stroke(ctx
->cr
);
529 cairo_pattern_destroy(pat
);
530 /* reset line-width */
531 cairo_set_line_width(ctx
->cr
, 1.0);
534 /** Draw an image from ARGB data to a draw context.
535 * Data should be stored as an array of alpha, red, blue, green for each pixel
536 * and the array size should be w * h elements long.
537 * \param ctx Draw context to draw to.
538 * \param x X coordinate.
539 * \param y Y coordinate.
542 * \param ratio The ratio to apply to the image.
543 * \param data The image pixels array.
546 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
547 double ratio
, unsigned char *data
)
550 cairo_surface_t
*source
;
552 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
553 #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)
554 sizeof(unsigned char) * 4 * w
);
556 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
558 cr
= cairo_create(ctx
->surface
);
559 cairo_scale(cr
, ratio
, ratio
);
560 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
565 cairo_surface_destroy(source
);
568 /** Draw an image to a draw context.
569 * \param ctx Draw context to draw to.
570 * \param x X coordinate.
571 * \param y Y coordinate.
572 * \param ratio The ratio to apply to the image.
573 * \param image The image to draw.
576 draw_image(draw_context_t
*ctx
, int x
, int y
, double ratio
, image_t
*image
)
578 draw_image_from_argb_data(ctx
, x
, y
, image_getwidth(image
), image_getheight(image
), ratio
, image_getdata(image
));
582 * \param ctx Draw context to draw with.
583 * \param src Drawable to draw from.
584 * \param dest Drawable to draw to.
585 * \param src_w Drawable width.
586 * \param src_h Drawable height.
587 * \param dest_w Drawable width.
588 * \param dest_h Drawable height.
589 * \param angle angle to rotate.
590 * \param tx Translate to this x coordinate.
591 * \param ty Translate to this y coordinate.
594 draw_rotate(draw_context_t
*ctx
,
595 xcb_pixmap_t src
, xcb_pixmap_t dest
,
596 int src_w
, int src_h
,
597 int dest_w
, int dest_h
,
598 double angle
, int tx
, int ty
)
600 cairo_surface_t
*surface
, *source
;
603 surface
= cairo_xcb_surface_create(globalconf
.connection
, dest
,
604 globalconf
.screens
.tab
[ctx
->phys_screen
].visual
,
606 source
= cairo_xcb_surface_create(globalconf
.connection
, src
,
607 globalconf
.screens
.tab
[ctx
->phys_screen
].visual
,
609 cr
= cairo_create (surface
);
611 cairo_translate(cr
, tx
, ty
);
612 cairo_rotate(cr
, angle
);
614 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
618 cairo_surface_destroy(source
);
619 cairo_surface_destroy(surface
);
622 /** Return the width and height of a text in pixel.
623 * \param data The draw context text data.
624 * \return Text height and width.
627 draw_text_extents(draw_text_context_t
*data
)
629 cairo_surface_t
*surface
;
633 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
634 area_t geom
= { 0, 0, 0, 0 };
639 surface
= cairo_xcb_surface_create(globalconf
.connection
,
640 globalconf
.default_screen
,
641 globalconf
.screens
.tab
[0].visual
,
643 s
->height_in_pixels
);
645 cr
= cairo_create(surface
);
646 layout
= pango_cairo_create_layout(cr
);
647 pango_layout_set_text(layout
, data
->text
, data
->len
);
648 pango_layout_set_attributes(layout
, data
->attr_list
);
649 pango_layout_set_font_description(layout
, globalconf
.font
->desc
);
650 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
651 g_object_unref(layout
);
653 cairo_surface_destroy(surface
);
655 geom
.width
= ext
.width
;
656 geom
.height
= ext
.height
;
661 /** Transform a string to a alignment_t type.
662 * Recognized string are flex, fixed, left, center, middle or right.
663 * \param align Atring with align text.
664 * \param len The string length.
665 * \return An alignment_t type.
668 draw_align_fromstr(const char *align
, ssize_t len
)
670 switch(a_tokenize(align
, len
))
672 case A_TK_CENTER
: return AlignCenter
;
673 case A_TK_RIGHT
: return AlignRight
;
674 case A_TK_TOP
: return AlignTop
;
675 case A_TK_BOTTOM
: return AlignBottom
;
676 case A_TK_MIDDLE
: return AlignMiddle
;
677 default: return AlignLeft
;
681 /** Transform an alignment to a string.
682 * \param a The alignment.
683 * \return A string which must not be freed.
686 draw_align_tostr(alignment_t a
)
690 case AlignLeft
: return "left";
691 case AlignCenter
: return "center";
692 case AlignRight
: return "right";
693 case AlignBottom
: return "bottom";
694 case AlignTop
: return "top";
695 case AlignMiddle
: return "middle";
696 default: return NULL
;
700 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80