draw: draw_text take extents as argument

This avoid one call of pango_layout_get_pixel_extents().

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-12-02 11:50:39 +01:00
parent 8442bb10ca
commit da99991223
3 changed files with 14 additions and 14 deletions

16
draw.c
View File

@ -337,13 +337,14 @@ draw_context_init(draw_context_t *d, int phys_screen,
* \param text Text to draw. * \param text Text to draw.
* \param len Text to draw length. * \param len Text to draw length.
* \param data Optional parser data. * \param data Optional parser data.
* \param ext Text extents.
*/ */
void void
draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrapMode wrap, draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrapMode wrap,
area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata) area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata,
area_t *ext)
{ {
int x, y; int x, y;
PangoRectangle ext;
draw_parser_data_t parser_data; draw_parser_data_t parser_data;
if(!pdata) if(!pdata)
@ -399,23 +400,22 @@ draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrap
pango_layout_set_wrap(ctx->layout, wrap); pango_layout_set_wrap(ctx->layout, wrap);
pango_layout_set_attributes(ctx->layout, pdata->attr_list); pango_layout_set_attributes(ctx->layout, pdata->attr_list);
pango_layout_set_font_description(ctx->layout, font->desc); pango_layout_set_font_description(ctx->layout, font->desc);
pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
x = area.x + pdata->margin.left; x = area.x + pdata->margin.left;
/* + 1 is added for rounding, so that in any case of doubt we rather draw /* + 1 is added for rounding, so that in any case of doubt we rather draw
* the text 1px lower than too high which usually results in a better type * the text 1px lower than too high which usually results in a better type
* face */ * face */
y = area.y + (ctx->height - ext.height + 1) / 2 + pdata->margin.top; y = area.y + (ctx->height - ext->height + 1) / 2 + pdata->margin.top;
/* only honors alignment if enough space */ /* only honors alignment if enough space */
if(ext.width < area.width) if(ext->width < area.width)
switch(pdata->align) switch(pdata->align)
{ {
case AlignCenter: case AlignCenter:
x += (area.width - ext.width) / 2; x += (area.width - ext->width) / 2;
break; break;
case AlignRight: case AlignRight:
x += area.width - ext.width; x += area.width - ext->width;
break; break;
default: default:
break; break;
@ -860,7 +860,7 @@ draw_text_extents(font_t *font, const char *text, ssize_t len, draw_parser_data_
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
geom.width = ext.width; geom.width = ext.width;
geom.height = ext.height * 1.5; geom.height = ext.height;
return geom; return geom;
} }

2
draw.h
View File

@ -177,7 +177,7 @@ typedef struct
} border; } border;
} draw_parser_data_t; } draw_parser_data_t;
void draw_text(draw_context_t *, font_t *, PangoEllipsizeMode, PangoWrapMode, area_t, const char *, ssize_t len, draw_parser_data_t *); void draw_text(draw_context_t *, font_t *, PangoEllipsizeMode, PangoWrapMode, area_t, const char *, ssize_t len, draw_parser_data_t *, area_t *);
void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *); void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *);
void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t, void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *); const xcolor_t *, const xcolor_t *, const xcolor_t *);

View File

@ -34,7 +34,7 @@ typedef struct
/** Textbox width */ /** Textbox width */
int width; int width;
/** Extents */ /** Extents */
int extents; area_t extents;
PangoEllipsizeMode ellip; PangoEllipsizeMode ellip;
PangoWrapMode wrap; PangoWrapMode wrap;
/** Draw parser data */ /** Draw parser data */
@ -55,7 +55,7 @@ textbox_geometry(widget_t *widget, int screen, int height, int width)
geometry.width = width; geometry.width = width;
else else
{ {
geometry.width = MIN(d->extents, width); geometry.width = MIN(d->extents.width, width);
if(d->pdata.bg_image) if(d->pdata.bg_image)
geometry.width = MAX(geometry.width, geometry.width = MAX(geometry.width,
@ -76,7 +76,7 @@ textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry,
int screen, wibox_t *p) int screen, wibox_t *p)
{ {
textbox_data_t *d = widget->data; textbox_data_t *d = widget->data;
draw_text(ctx, globalconf.font, d->ellip, d->wrap, geometry, d->text, d->len, &d->pdata); draw_text(ctx, globalconf.font, d->ellip, d->wrap, geometry, d->text, d->len, &d->pdata, &d->extents);
} }
/** Delete a textbox widget. /** Delete a textbox widget.
@ -178,10 +178,10 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token)
if(buf) if(buf)
{ {
a_iso2utf8(&d->text, buf, len); a_iso2utf8(&d->text, buf, len);
d->extents = draw_text_extents(globalconf.font, d->text, d->len, &d->pdata).width; d->extents = draw_text_extents(globalconf.font, d->text, d->len, &d->pdata);
} }
else else
d->extents = 0; p_clear(&d->extents, 1);
} }
break; break;
case A_TK_WIDTH: case A_TK_WIDTH: