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>
32 #include "globalconf.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 /** Initialize a draw_text_context_t with text data.
93 * \param data The draw text context to init.
94 * \param str The text string to render.
95 * \param slen The text string length.
96 * \return True if everything is ok, false otherwise.
99 draw_text_context_init(draw_text_context_t
*data
, const char *str
, ssize_t slen
)
101 GError
*error
= NULL
;
106 if(!pango_parse_markup(str
, slen
, 0, &data
->attr_list
, &data
->text
, NULL
, &error
))
108 warn("cannot parse pango markup: %s", error
? error
->message
: "unknown error");
114 data
->len
= a_strlen(data
->text
);
119 /** Initialize a new draw context.
120 * \param d The draw context to initialize.
121 * \param phys_screen Physical screen id.
122 * \param width Width.
123 * \param height Height.
124 * \param px Pixmap object to store.
125 * \param fg Foreground color.
126 * \param bg Background color.
129 draw_context_init(draw_context_t
*d
, int phys_screen
,
130 int width
, int height
, xcb_pixmap_t px
,
131 const xcolor_t
*fg
, const xcolor_t
*bg
)
133 d
->phys_screen
= phys_screen
;
137 d
->surface
= cairo_xcb_surface_create(globalconf
.connection
,
138 px
, globalconf
.screens
.tab
[phys_screen
].visual
,
140 d
->cr
= cairo_create(d
->surface
);
141 d
->layout
= pango_cairo_create_layout(d
->cr
);
146 /** Draw text into a draw context.
147 * \param ctx Draw context to draw to.
148 * \param data Draw text context data.
149 * \param ellip Ellipsize mode.
150 * \param wrap Wrap mode.
151 * \param align Text alignment.
152 * \param valign Vertical text alignment.
153 * \param area Area to draw to.
156 draw_text(draw_context_t
*ctx
, draw_text_context_t
*data
,
157 PangoEllipsizeMode ellip
, PangoWrapMode wrap
,
158 alignment_t align
, alignment_t valign
, area_t area
)
160 pango_layout_set_text(ctx
->layout
, data
->text
, data
->len
);
161 pango_layout_set_width(ctx
->layout
,
162 pango_units_from_double(area
.width
));
163 pango_layout_set_height(ctx
->layout
, pango_units_from_double(area
.height
));
164 pango_layout_set_ellipsize(ctx
->layout
, ellip
);
165 pango_layout_set_wrap(ctx
->layout
, wrap
);
166 pango_layout_set_attributes(ctx
->layout
, data
->attr_list
);
167 pango_layout_set_font_description(ctx
->layout
, globalconf
.font
->desc
);
170 pango_layout_get_pixel_extents(ctx
->layout
, NULL
, &ext
);
175 area
.x
+= (area
.width
- ext
.width
) / 2;
178 area
.x
+= area
.width
- ext
.width
;
187 area
.y
+= (area
.height
- ext
.height
) / 2;
190 area
.y
+= area
.height
- ext
.height
;
196 cairo_move_to(ctx
->cr
, area
.x
, area
.y
);
198 cairo_set_source_rgba(ctx
->cr
,
199 ctx
->fg
.red
/ 65535.0,
200 ctx
->fg
.green
/ 65535.0,
201 ctx
->fg
.blue
/ 65535.0,
202 ctx
->fg
.alpha
/ 65535.0);
203 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
204 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
207 /** Setup color-source for cairo (gradient or mono).
208 * \param ctx Draw context.
209 * \param gradient_vector x, y to x + x_offset, y + y_offset.
210 * \param pcolor Color to use at start of gradient_vector.
211 * \param pcolor_center Color at center of gradient_vector.
212 * \param pcolor_end Color at end of gradient_vector.
213 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
215 static cairo_pattern_t
*
216 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
217 const color_t
*pcolor
, const color_t
*pcolor_center
,
218 const color_t
*pcolor_end
)
220 cairo_pattern_t
*pat
= NULL
;
221 bool has_center
= pcolor_center
->initialized
;
222 bool has_end
= pcolor_end
->initialized
;
224 /* no need for a real pattern: */
225 if(!has_end
&& !has_center
)
226 cairo_set_source_rgba(ctx
->cr
,
228 pcolor
->green
/ 255.0,
229 pcolor
->blue
/ 255.0,
230 pcolor
->alpha
/ 255.0);
233 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
235 gradient_vector
.x
+ gradient_vector
.x_offset
,
236 gradient_vector
.y
+ gradient_vector
.y_offset
);
238 /* pcolor is always set (so far in awesome) */
239 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
241 pcolor
->green
/ 255.0,
242 pcolor
->blue
/ 255.0,
243 pcolor
->alpha
/ 255.0);
246 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
247 pcolor_center
->red
/ 255.0,
248 pcolor_center
->green
/ 255.0,
249 pcolor_center
->blue
/ 255.0,
250 pcolor_center
->alpha
/ 255.0);
253 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
254 pcolor_end
->red
/ 255.0,
255 pcolor_end
->green
/ 255.0,
256 pcolor_end
->blue
/ 255.0,
257 pcolor_end
->alpha
/ 255.0);
259 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
261 pcolor
->green
/ 255.0,
262 pcolor
->blue
/ 255.0,
263 pcolor
->alpha
/ 255.0);
264 cairo_set_source(ctx
->cr
, pat
);
269 /** Draw rectangle inside the coordinates
270 * \param ctx Draw context
271 * \param geometry geometry
272 * \param line_width line width
273 * \param filled fill rectangle?
274 * \param color color to use
277 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
278 float line_width
, bool filled
, const color_t
*color
)
280 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
281 cairo_set_line_width(ctx
->cr
, line_width
);
282 cairo_set_miter_limit(ctx
->cr
, 10.0);
283 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
284 cairo_set_source_rgba(ctx
->cr
,
286 color
->green
/ 255.0,
288 color
->alpha
/ 255.0);
291 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
292 geometry
.width
, geometry
.height
);
297 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
298 geometry
.width
- line_width
, geometry
.height
- line_width
);
299 cairo_stroke(ctx
->cr
);
303 /** Draw rectangle with gradient colors
304 * \param ctx Draw context.
305 * \param geometry Geometry.
306 * \param line_width Line width.
307 * \param filled Filled rectangle?
308 * \param gradient_vector Color-gradient course.
309 * \param pcolor Color at start of gradient_vector.
310 * \param pcolor_center Color in the center.
311 * \param pcolor_end Color at end of gradient_vector.
314 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
315 vector_t gradient_vector
, const color_t
*pcolor
,
316 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
318 cairo_pattern_t
*pat
;
320 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
321 cairo_set_line_width(ctx
->cr
, line_width
);
322 cairo_set_miter_limit(ctx
->cr
, 10.0);
323 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
325 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
329 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
334 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
335 cairo_stroke(ctx
->cr
);
339 cairo_pattern_destroy(pat
);
342 /** Setup some cairo-things for drawing a graph
343 * \param ctx Draw context
346 draw_graph_setup(draw_context_t
*ctx
)
348 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
349 cairo_set_line_width(ctx
->cr
, 1.0);
350 /* without it, it can draw over the path on sharp angles
351 * ...too long lines * (...graph_line) */
352 cairo_set_miter_limit(ctx
->cr
, 0.0);
353 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
357 * \param ctx Draw context.
358 * \param rect The area to draw into.
359 * \param from Array of starting-point offsets to draw a graph lines.
360 * \param to Array of end-point offsets to draw a graph lines.
361 * \param cur_index Current position in data-array (cycles around).
362 * \param grow Put new values to the left or to the right.
363 * \param gradient_vector Color-Gradient course.
364 * \param pcolor Color at start of gradient_vector.
365 * \param pcolor_center Color in the center.
366 * \param pcolor_end Color at end of gradient_vector.
369 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
370 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
371 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
374 float x
= rect
.x
+ 0.5; /* middle of a pixel */
375 cairo_pattern_t
*pat
;
377 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
378 pcolor
, pcolor_center
, pcolor_end
);
380 if(grow
== Right
) /* draw from right to left */
383 while(++i
< rect
.width
)
385 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
386 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
390 cur_index
= rect
.width
- 1;
393 else /* draw from left to right */
394 while(++i
< rect
.width
)
396 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
397 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
401 cur_index
= rect
.width
- 1;
404 cairo_stroke(ctx
->cr
);
407 cairo_pattern_destroy(pat
);
410 /** Draw a line into a graph-widget.
411 * \param ctx Draw context.
412 * \param rect The area to draw into.
413 * \param to array of offsets to draw the line through...
414 * \param cur_index current position in data-array (cycles around)
415 * \param grow put new values to the left or to the right
416 * \param gradient_vector Color-gradient course.
417 * \param pcolor Color at start of gradient_vector.
418 * \param pcolor_center Color in the center.
419 * \param pcolor_end Color at end of gradient_vector.
422 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
423 position_t grow
, vector_t gradient_vector
, const color_t
*pcolor
,
424 const color_t
*pcolor_center
, const color_t
*pcolor_end
)
428 cairo_pattern_t
*pat
;
430 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
431 * ... it won't fill some pixels! It also looks much nicer so.
432 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
433 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
434 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
435 /* a nicer, better visible line compared to 1.0 */
436 cairo_set_line_width(ctx
->cr
, 1.25);
438 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
440 /* path through the centers of pixels */
447 /* go through the values from old to new. Begin with the oldest. */
448 if(++cur_index
> w
- 1)
451 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
454 /* on the left border: fills a pixel also when there's only one value */
455 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
457 for(i
= 0; i
< w
; i
++)
459 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
462 /* cycles around the index */
465 if(++cur_index
> w
- 1)
475 /* onto the right border: fills a pixel also when there's only one value */
477 cairo_line_to(ctx
->cr
, x
, y
- to
[(cur_index
+ (w
- 1)) % w
]);
479 cairo_stroke(ctx
->cr
);
482 cairo_pattern_destroy(pat
);
483 /* reset line-width */
484 cairo_set_line_width(ctx
->cr
, 1.0);
487 /** Draw an image from ARGB data to a draw context.
488 * Data should be stored as an array of alpha, red, blue, green for each pixel
489 * and the array size should be w * h elements long.
490 * \param ctx Draw context to draw to.
491 * \param x X coordinate.
492 * \param y Y coordinate.
495 * \param ratio The ratio to apply to the image.
496 * \param data The image pixels array.
499 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
500 double ratio
, unsigned char *data
)
503 cairo_surface_t
*source
;
505 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
506 #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)
507 sizeof(unsigned char) * 4 * w
);
509 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
511 cr
= cairo_create(ctx
->surface
);
512 cairo_scale(cr
, ratio
, ratio
);
513 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
518 cairo_surface_destroy(source
);
521 /** Draw an image to a draw context.
522 * \param ctx Draw context to draw to.
523 * \param x X coordinate.
524 * \param y Y coordinate.
525 * \param ratio The ratio to apply to the image.
526 * \param image The image to draw.
529 draw_image(draw_context_t
*ctx
, int x
, int y
, double ratio
, image_t
*image
)
531 draw_image_from_argb_data(ctx
, x
, y
, image_getwidth(image
), image_getheight(image
), ratio
, image_getdata(image
));
535 * \param ctx Draw context to draw with.
536 * \param src Drawable to draw from.
537 * \param dest Drawable to draw to.
538 * \param src_w Drawable width.
539 * \param src_h Drawable height.
540 * \param dest_w Drawable width.
541 * \param dest_h Drawable height.
542 * \param angle angle to rotate.
543 * \param tx Translate to this x coordinate.
544 * \param ty Translate to this y coordinate.
547 draw_rotate(draw_context_t
*ctx
,
548 xcb_pixmap_t src
, xcb_pixmap_t dest
,
549 int src_w
, int src_h
,
550 int dest_w
, int dest_h
,
551 double angle
, int tx
, int ty
)
553 cairo_surface_t
*surface
, *source
;
556 surface
= cairo_xcb_surface_create(globalconf
.connection
, dest
,
557 globalconf
.screens
.tab
[ctx
->phys_screen
].visual
,
559 source
= cairo_xcb_surface_create(globalconf
.connection
, src
,
560 globalconf
.screens
.tab
[ctx
->phys_screen
].visual
,
562 cr
= cairo_create (surface
);
564 cairo_translate(cr
, tx
, ty
);
565 cairo_rotate(cr
, angle
);
567 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
571 cairo_surface_destroy(source
);
572 cairo_surface_destroy(surface
);
575 /** Return the width and height of a text in pixel.
576 * \param data The draw context text data.
577 * \return Text height and width.
580 draw_text_extents(draw_text_context_t
*data
)
582 cairo_surface_t
*surface
;
586 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
587 area_t geom
= { 0, 0, 0, 0 };
592 surface
= cairo_xcb_surface_create(globalconf
.connection
,
593 globalconf
.default_screen
,
594 globalconf
.screens
.tab
[0].visual
,
596 s
->height_in_pixels
);
598 cr
= cairo_create(surface
);
599 layout
= pango_cairo_create_layout(cr
);
600 pango_layout_set_text(layout
, data
->text
, data
->len
);
601 pango_layout_set_attributes(layout
, data
->attr_list
);
602 pango_layout_set_font_description(layout
, globalconf
.font
->desc
);
603 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
604 g_object_unref(layout
);
606 cairo_surface_destroy(surface
);
608 geom
.width
= ext
.width
;
609 geom
.height
= ext
.height
;
614 /** Transform a string to a alignment_t type.
615 * Recognized string are flex, fixed, left, center, middle or right.
616 * \param align A string with align text.
617 * \param len The string length.
618 * \return An alignment_t type.
621 draw_align_fromstr(const char *align
, ssize_t len
)
623 switch(a_tokenize(align
, len
))
625 case A_TK_CENTER
: return AlignCenter
;
626 case A_TK_RIGHT
: return AlignRight
;
627 case A_TK_TOP
: return AlignTop
;
628 case A_TK_BOTTOM
: return AlignBottom
;
629 case A_TK_MIDDLE
: return AlignMiddle
;
630 default: return AlignLeft
;
634 /** Transform an alignment to a string.
635 * \param a The alignment.
636 * \return A string which must not be freed.
639 draw_align_tostr(alignment_t a
)
643 case AlignLeft
: return "left";
644 case AlignCenter
: return "center";
645 case AlignRight
: return "right";
646 case AlignBottom
: return "bottom";
647 case AlignTop
: return "top";
648 case AlignMiddle
: return "middle";
649 default: return NULL
;
653 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80