Centered text for buttons

This commit is contained in:
Kevin Lange 2012-04-17 13:55:54 -05:00
parent efcae21316
commit 0b6045918b
3 changed files with 25 additions and 1 deletions

View File

@ -51,7 +51,9 @@ void ttk_render_button(void * s) {
draw_line(ctx, self->x + self->width, self->x + self->width, self->y, self->y + self->height, border_color);
draw_line(ctx, self->x, self->x + self->width, self->y + self->height, self->y + self->height, border_color);
/* button-specific stuff */
draw_string(ctx, self->x + 10, self->y + self->height - 3, ((ttk_button *)self)->fore_color, ((ttk_button * )self)->title);
uint32_t w = draw_string_width(((ttk_button * )self)->title);
uint16_t offset = (self->width - w) / 2;
draw_string(ctx, self->x + offset, self->y + self->height - 3, ((ttk_button *)self)->fore_color, ((ttk_button * )self)->title);
}
ttk_button * ttk_new_button(char * title, void (*callback)(void *, w_mouse_t *)) {

View File

@ -56,6 +56,27 @@ static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, gfx_context
}
}
uint32_t draw_string_width(char * string) {
slot = face->glyph;
int pen_x = 0, i = 0;
int len = strlen(string);
int error;
for (i = 0; i < len; ++i) {
FT_UInt glyph_index;
glyph_index = FT_Get_Char_Index( face, string[i]);
error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if (error) {
printf("Error loading glyph for '%c'\n", string[i]);
continue;
}
slot = (face)->glyph;
pen_x += slot->advance.x >> 6;
}
return pen_x;
}
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string) {
slot = face->glyph;
int pen_x = x, pen_y = y, i = 0;

View File

@ -6,5 +6,6 @@
void init_shmemfonts();
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string);
uint32_t draw_string_width(char * string);
#endif