From 1b4a1e7c9ee3a37bc751c202cfcff1946a18ac36 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 4 Feb 2008 13:28:20 +0100 Subject: [PATCH] convert text to draw to UTF-8 --- common/draw.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- common/draw.h | 2 +- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/common/draw.c b/common/draw.c index 082883ec..8ac5b29b 100644 --- a/common/draw.c +++ b/common/draw.c @@ -22,10 +22,56 @@ #include #include #include + +#include +#include +#include + #include + #include "draw.h" #include "common/util.h" +static char * +draw_iso2utf8(char *iso) +{ + iconv_t iso2utf8; + size_t len, utf8len; + char *utf8; + + if(!(len = a_strlen(iso))) + return NULL; + + if(!a_strcmp(nl_langinfo(CODESET), "UTF-8")) + return iso; + + iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET)); + if(iso2utf8 == (iconv_t) -1) + { + if(errno == EINVAL) + warn("unable to convert text from %s to UTF-8, not available", + nl_langinfo(CODESET)); + else + perror("awesome: unable to convert text"); + + return NULL; + } + + utf8len = (3 * len) / 2 + 1; + utf8 = p_new(char, utf8len); + + if(iconv(iso2utf8, &iso, &len, &utf8, &utf8len) == (size_t) -1) + { + perror("awesome: text conversion failed"); + return NULL; + } + + if(iconv_close(iso2utf8)) + warn("error closing iconv"); + + return utf8; +} + /** Get a draw context * \param phys_screen physical screen id * \param width width @@ -75,7 +121,7 @@ draw_text(DrawCtx *ctx, Area area, Alignment align, int padding, - XftFont *font, const char *text, + XftFont *font, char *text, XColor fg, XColor bg) { int nw = 0; @@ -90,6 +136,9 @@ draw_text(DrawCtx *ctx, if(!len) return; + /* try to convert text to UTF-8 */ + text = draw_iso2utf8(text); + font_face = cairo_ft_font_face_create_for_pattern(font->pattern); cairo_set_font_face(ctx->cr, font_face); cairo_set_font_size(ctx->cr, font->height); diff --git a/common/draw.h b/common/draw.h index 00afbd7f..6150268e 100644 --- a/common/draw.h +++ b/common/draw.h @@ -93,7 +93,7 @@ typedef struct DrawCtx *draw_context_new(Display *, int, int, int, Drawable); void draw_context_delete(DrawCtx *); -void draw_text(DrawCtx *, Area, Alignment, int, XftFont *, const char *, XColor fg, XColor bg); +void draw_text(DrawCtx *, Area, Alignment, int, XftFont *, char *, XColor fg, XColor bg); void draw_rectangle(DrawCtx *, Area, Bool, XColor); void draw_rectangle_gradient(DrawCtx *, Area, int, Bool, XColor, XColor);