From 210fc6dfed0917f44e3fe4d5fb56c9ede0edc03f Mon Sep 17 00:00:00 2001 From: Quentin Glidic Date: Tue, 7 Aug 2012 22:30:37 +0200 Subject: [PATCH] libi3: Rework predict_text_width predict_text_width now takes an i3String as argument --- i3-input/main.c | 2 +- i3bar/src/workspaces.c | 5 ++--- i3bar/src/xcb.c | 2 +- include/libi3.h | 6 +++--- libi3/font.c | 34 ++++++++++++++++------------------ src/sighandler.c | 2 +- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/i3-input/main.c b/i3-input/main.c index 6f5ad78a..2de5a41e 100644 --- a/i3-input/main.c +++ b/i3-input/main.c @@ -333,7 +333,7 @@ int main(int argc, char *argv[]) { sockfd = ipc_connect(socket_path); if (prompt != NULL) - prompt_offset = predict_text_width((char *)i3string_as_ucs2(prompt), i3string_get_num_glyphs(prompt), true); + prompt_offset = predict_text_width(prompt); int screens; conn = xcb_connect(NULL, &screens); diff --git a/i3bar/src/workspaces.c b/i3bar/src/workspaces.c index c77103f3..6db37983 100644 --- a/i3bar/src/workspaces.c +++ b/i3bar/src/workspaces.c @@ -116,10 +116,9 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, unsigne /* Save the name */ params->workspaces_walk->name = i3string_from_utf8_with_length((const char *)val, len); - /* Convert the name to ucs2, save its length in glyphs and calculate its rendered width */ + /* Save its rendered width */ params->workspaces_walk->name_width = - predict_text_width((char *)i3string_as_ucs2(params->workspaces_walk->name), - i3string_get_num_glyphs(params->workspaces_walk->name), true); + predict_text_width(params->workspaces_walk->name); DLOG("Got Workspace %s, name_width: %d, glyphs: %zu\n", i3string_as_utf8(params->workspaces_walk->name), diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c index 729803e1..06b3fb9a 100644 --- a/i3bar/src/xcb.c +++ b/i3bar/src/xcb.c @@ -119,7 +119,7 @@ void refresh_statusline() { if (i3string_get_num_bytes(block->full_text) == 0) continue; - block->width = predict_text_width((char *)i3string_as_ucs2(block->full_text), i3string_get_num_glyphs(block->full_text), true); + block->width = predict_text_width(block->full_text); /* If this is not the last block, add some pixels for a separator. */ if (TAILQ_NEXT(block, blocks) != NULL) block->width += 9; diff --git a/include/libi3.h b/include/libi3.h index 29b8c107..01b9992b 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -311,11 +311,11 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable, xcb_gcontext_t gc, int x, int y, int max_width); /** - * Predict the text width in pixels for the given text. Text can be specified - * as UCS-2 or UTF-8. + * Predict the text width in pixels for the given text. Text must be + * specified as an i3String. * */ -int predict_text_width(char *text, size_t text_len, bool is_ucs2); +int predict_text_width(i3String *text); /** * Returns true if this version of i3 is a debug build (anything which is not a diff --git a/libi3/font.c b/libi3/font.c index 853e5f92..823888f2 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -105,6 +105,8 @@ void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background xcb_change_gc(conn, gc, mask, values); } +static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len); + static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable, xcb_gcontext_t gc, int x, int y, int max_width) { /* X11 coordinates for fonts start at the baseline */ @@ -130,7 +132,7 @@ static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawabl break; /* Advance pos_x based on the predicted text width */ - x += predict_text_width((char*)chunk, chunk_size, true); + x += predict_text_width_xcb(chunk, chunk_size); } } @@ -172,7 +174,7 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable, } } -static int xcb_query_text_width(xcb_char2b_t *text, size_t text_len) { +static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) { /* Make the user know we’re using the slow path, but only once. */ static bool first_invocation = true; if (first_invocation) { @@ -199,18 +201,9 @@ static int xcb_query_text_width(xcb_char2b_t *text, size_t text_len) { return width; } -/* - * Predict the text width in pixels for the given text. Text can be specified - * as UCS-2 or UTF-8. - * - */ -int predict_text_width(char *text, size_t text_len, bool is_ucs2) { - /* Convert the text into UTF-16 so we can do basic pointer math */ - xcb_char2b_t *input; - if (is_ucs2) - input = (xcb_char2b_t*)text; - else - input = convert_utf8_to_ucs2(text, &text_len); +static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) { + if (text_len == 0) + return 0; int width; if (savedFont->table == NULL) { @@ -249,9 +242,14 @@ int predict_text_width(char *text, size_t text_len, bool is_ucs2) { } } - /* If we had to convert, free the converted string */ - if (!is_ucs2) - free(input); - return width; } + +/* + * Predict the text width in pixels for the given text. Text must be + * specified as an i3String. + * + */ +int predict_text_width(i3String *text) { + return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text)); +} diff --git a/src/sighandler.c b/src/sighandler.c index e9a4e9bf..3a9307e1 100644 --- a/src/sighandler.c +++ b/src/sighandler.c @@ -155,7 +155,7 @@ void handle_signal(int sig, siginfo_t *info, void *data) { } crash_text_i3strings[crash_text_length] = NULL; /* calculate width for longest text */ - int font_width = predict_text_width((char *)i3string_as_ucs2(crash_text_i3strings[crash_text_longest]), i3string_get_num_glyphs(crash_text_i3strings[crash_text_longest]), true); + int font_width = predict_text_width(crash_text_i3strings[crash_text_longest]); int width = font_width + 20; /* Open a popup window on each virtual screen */ -- 2.39.5