convert text to draw to UTF-8

This commit is contained in:
Julien Danjou 2008-02-04 13:28:20 +01:00
parent 9fc7d714c2
commit 1b4a1e7c9e
2 changed files with 51 additions and 2 deletions

View File

@ -22,10 +22,56 @@
#include <cairo.h> #include <cairo.h>
#include <cairo-ft.h> #include <cairo-ft.h>
#include <cairo-xlib.h> #include <cairo-xlib.h>
#include <langinfo.h>
#include <iconv.h>
#include <errno.h>
#include <math.h> #include <math.h>
#include "draw.h" #include "draw.h"
#include "common/util.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 /** Get a draw context
* \param phys_screen physical screen id * \param phys_screen physical screen id
* \param width width * \param width width
@ -75,7 +121,7 @@ draw_text(DrawCtx *ctx,
Area area, Area area,
Alignment align, Alignment align,
int padding, int padding,
XftFont *font, const char *text, XftFont *font, char *text,
XColor fg, XColor bg) XColor fg, XColor bg)
{ {
int nw = 0; int nw = 0;
@ -90,6 +136,9 @@ draw_text(DrawCtx *ctx,
if(!len) if(!len)
return; return;
/* try to convert text to UTF-8 */
text = draw_iso2utf8(text);
font_face = cairo_ft_font_face_create_for_pattern(font->pattern); font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
cairo_set_font_face(ctx->cr, font_face); cairo_set_font_face(ctx->cr, font_face);
cairo_set_font_size(ctx->cr, font->height); cairo_set_font_size(ctx->cr, font->height);

View File

@ -93,7 +93,7 @@ typedef struct
DrawCtx *draw_context_new(Display *, int, int, int, Drawable); DrawCtx *draw_context_new(Display *, int, int, int, Drawable);
void draw_context_delete(DrawCtx *); 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(DrawCtx *, Area, Bool, XColor);
void draw_rectangle_gradient(DrawCtx *, Area, int, Bool, XColor, XColor); void draw_rectangle_gradient(DrawCtx *, Area, int, Bool, XColor, XColor);