libi3: Rework predict_text_width

predict_text_width now takes an i3String as argument
next
Quentin Glidic 2012-08-07 22:30:37 +02:00
parent 53365fa887
commit 210fc6dfed
6 changed files with 24 additions and 27 deletions

View File

@ -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);

View File

@ -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),

View File

@ -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;

View File

@ -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

View File

@ -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 were 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));
}

View File

@ -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 */