libi3: Rework predict_text_width
predict_text_width now takes an i3String as argument
This commit is contained in:
parent
53365fa887
commit
210fc6dfed
|
@ -333,7 +333,7 @@ int main(int argc, char *argv[]) {
|
||||||
sockfd = ipc_connect(socket_path);
|
sockfd = ipc_connect(socket_path);
|
||||||
|
|
||||||
if (prompt != NULL)
|
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;
|
int screens;
|
||||||
conn = xcb_connect(NULL, &screens);
|
conn = xcb_connect(NULL, &screens);
|
||||||
|
|
|
@ -116,10 +116,9 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, unsigne
|
||||||
/* Save the name */
|
/* Save the name */
|
||||||
params->workspaces_walk->name = i3string_from_utf8_with_length((const char *)val, len);
|
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 =
|
params->workspaces_walk->name_width =
|
||||||
predict_text_width((char *)i3string_as_ucs2(params->workspaces_walk->name),
|
predict_text_width(params->workspaces_walk->name);
|
||||||
i3string_get_num_glyphs(params->workspaces_walk->name), true);
|
|
||||||
|
|
||||||
DLOG("Got Workspace %s, name_width: %d, glyphs: %zu\n",
|
DLOG("Got Workspace %s, name_width: %d, glyphs: %zu\n",
|
||||||
i3string_as_utf8(params->workspaces_walk->name),
|
i3string_as_utf8(params->workspaces_walk->name),
|
||||||
|
|
|
@ -119,7 +119,7 @@ void refresh_statusline() {
|
||||||
if (i3string_get_num_bytes(block->full_text) == 0)
|
if (i3string_get_num_bytes(block->full_text) == 0)
|
||||||
continue;
|
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 this is not the last block, add some pixels for a separator. */
|
||||||
if (TAILQ_NEXT(block, blocks) != NULL)
|
if (TAILQ_NEXT(block, blocks) != NULL)
|
||||||
block->width += 9;
|
block->width += 9;
|
||||||
|
|
|
@ -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);
|
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
|
* Predict the text width in pixels for the given text. Text must be
|
||||||
* as UCS-2 or UTF-8.
|
* 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
|
* Returns true if this version of i3 is a debug build (anything which is not a
|
||||||
|
|
34
libi3/font.c
34
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);
|
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,
|
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) {
|
xcb_gcontext_t gc, int x, int y, int max_width) {
|
||||||
/* X11 coordinates for fonts start at the baseline */
|
/* 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;
|
break;
|
||||||
|
|
||||||
/* Advance pos_x based on the predicted text width */
|
/* 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. */
|
/* Make the user know we’re using the slow path, but only once. */
|
||||||
static bool first_invocation = true;
|
static bool first_invocation = true;
|
||||||
if (first_invocation) {
|
if (first_invocation) {
|
||||||
|
@ -199,18 +201,9 @@ static int xcb_query_text_width(xcb_char2b_t *text, size_t text_len) {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) {
|
||||||
* Predict the text width in pixels for the given text. Text can be specified
|
if (text_len == 0)
|
||||||
* as UCS-2 or UTF-8.
|
return 0;
|
||||||
*
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
|
|
||||||
int width;
|
int width;
|
||||||
if (savedFont->table == NULL) {
|
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;
|
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));
|
||||||
|
}
|
||||||
|
|
|
@ -155,7 +155,7 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
|
||||||
}
|
}
|
||||||
crash_text_i3strings[crash_text_length] = NULL;
|
crash_text_i3strings[crash_text_length] = NULL;
|
||||||
/* calculate width for longest text */
|
/* 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;
|
int width = font_width + 20;
|
||||||
|
|
||||||
/* Open a popup window on each virtual screen */
|
/* Open a popup window on each virtual screen */
|
||||||
|
|
Loading…
Reference in New Issue